怎么纠正? - “都定义了getObjectCopy(),但是使用了不相关的返回类型” - 但它是* one *功能 [英] How to rectify? -- "both define getObjectCopy(), but with unrelated return types" -- but it's *one* function

查看:93
本文介绍了怎么纠正? - “都定义了getObjectCopy(),但是使用了不相关的返回类型” - 但它是* one *功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下界面heirarchy(删除了所有不相关的功能)。我在尝试编译时遇到此错误:

I have the following interface heirarchy (with all non-relevant functions stripped out). I am getting this error when trying to compile it:

类型ValidLineGettable和ValidateValue< java.lang.String>是不相容的;两者都定义了getObjectCopy(),但是使用了不相关的返回类型

这都来自同一个函数 - 而不是两个不同的函数name,同一个界面中的相同功能。如何处理 必须 自身 必须从单一基本接口继承的两个不同接口继承的接口?

This is all derived from the same function--not two different functions with the same name, the same function in the same interface. How do you deal with interfaces that must inherit from two different interfaces that themselves must inherit from a singular base interface?

我发现有两个关于不同功能碰巧有相同名称的SO问题

There are two SO questions I've found regarding different functions that happen to have the same name

  • Inherit method with unrelated return types
  • Java - Method name collision in interface implementation

在我的情况下,它在概念和名称上都是相同的功能。

In my case it's the same function, both conceptually and in name.

(虽然我对如果可复制的界面是一个好主意开始的意见感兴趣......它在很多我使用的代码,并且对我来说效果很好......我最感兴趣的是t一般继承/设计问题。)

(Although I am interested in opinions on if the Copyable interface is a good idea to begin with...it's in a lot of code that I use, and has worked well for me...I am mostly interested in the general inheritance/design question.)

我不清楚如何最好地处理这个问题。我很感激任何建议。谢谢。

I am not clear on how to best deal with this. I would appreciate any advice. Thank you.

public interface Copyable  {
   Copyable getObjectCopy();
}
interface ValidateValue<O> extends Copyable  {

   //Other functions...

   @Override
   ValidateValue<O> getObjectCopy();
}

//For classes that may be able to be Decorated into a TextLineValidator
interface ValidLineGettable extends Copyable  {

   //Other functions...

   ValidLineGettable getObjectCopy();
}
interface TextLineValidator extends ValidateValue<String>, ValidLineGettable  {

   //Other functions...

   @Override
   TextLineValidator getObjectCopy();
}

错误:

C:\java_code\Copyable.java:17: types ValidLineGettable and ValidateValue<java.lang.String> are incompatible; both define getObjectCopy(), but with unrelated return types
     interface TextLineValidator extends ValidateValue<String>, ValidLineGettable  {
     ^
 1 error

 Tool completed with exit code 1


推荐答案

假设所有返回值都延伸可复制,让所有版本的getObjectCopy()都返回Copyable。例如:

Assuming all the return values extend Copyable, have all versions of getObjectCopy() return Copyable. For example:

public interface ValidateValue<O> extends Copyable
{
     // Other functions...

     @Override
     Copyable getObjectCopy();
}

public Blammy implements ValidateValue<String>
{
    // Other functions...

     @Override
    public Copyable getObjectCopy()
    {
        SomethingThatExtendsCopyable blammy = new SomethingThatExtendsCopyable();

        return (Copyable)blammy;
    }
}



编辑



在上面的代码中,错误是由于getObjectCopy方法在 ValidateValue< String> 和<$ c中具有不同的返回值$ c> ValidLineGettable 接口,但调用签名是相同的。在java中,通过仅更改返回值不会获得多态性;这会导致编译错误。

Edit

In your code above the error is caused by the fact that the "getObjectCopy" method has a different return value in the ValidateValue<String> and ValidLineGettable interfaces, but the calling signature is the same. In java, you do not get polymorphism by changing only the return value; this results in a compile error.

如果将返回值更改为可复制,则 TextLineValidator 不再通过扩展其两个父接口来获得价值。一种更简单的方法是使用一个接口(可复制)和多个实现该接口的类,每个类都返回一个Copyable值,该值可以是扩展(或实现)Copyable的类的实例。

If you change the return value to Copyable then the TextLineValidator no longer gains value by extending both of its parent interfaces. A simpler approach is to have one interface (Copyable) and multiple classes that implement that interface, each of which returns a Copyable value which may be an instance of a class that extends (or implements) Copyable.

这篇关于怎么纠正? - “都定义了getObjectCopy(),但是使用了不相关的返回类型” - 但它是* one *功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆