java - Name Clash, override fail, on a class 实现两个具有相同删除的接口(interface)

标签 java generics inheritance polymorphism multiple-inheritance

我正在创建一个覆盖方法签名的类,该方法签名的删除在 2 个已实现的接口(interface)之间是相同的,但在泛型类型方面有细微差别(一个是方法推断类型,另一个是推断类类型) .我正在寻找一个简洁的解决方案。我只能编辑继承的类,不能编辑原始的遗留接口(interface)。

为了展示案例,我制作了一个抽象示例,以理解问题:

我有一个 Developer 遗留父类:

public class Developer<C>{
    Rate<C> getRate(Taxes<C> tax){ /*...*/ }     
}

我还有一个 Rentable 遗留接口(interface),具有几乎相同的签名

public interface Rentable {
    <C> Rate<C> getRate(Taxes<C> taxes);  
}

由于开发人员不可出租,在我的模型中,我创建了一个特殊的 developer 既是开发人员又是可出租 Material 。

public class OutsourcableDeveloper<C> 
                 extends Developer<C> 
                 implements Rentable{
   @Override
   public Rate<C> getRate(Taxes<C> taxes){ /*...*/}
}

然后我得到了臭名昭著的

Name clash: The method getRate(Developer.Taxes) of type OutsourcableDeveloper has the same erasure as getRate(Developer.Taxes) of type Rentable but does not override it

我怎样才能摆脱它,所以 OutsourcableDeveloper.getRate() 隐藏了 开发商和可出租的。 getRate()?

似乎有点不合逻辑,让一个通用的覆盖失败,然后又不允许扩展两个签名,因为删除是相等的。

当我不打算在我的实现中调用任何 super 时,其中一个父类(super class)型从 de 方法推断类型而另一个从类推断类型真的很重要吗?考虑到这种简化,是否有解决此问题的技巧?

编辑:我针对我的实际问题打开了一个更抽象、更少面向解决方案的问题来讨论继承设计问题,我认为这是我遇到的实际问题的相关本质:Why can't I extend an interface "generic method" and narrow its type to my inherited interface "class generic"?

EDIT2:上一个问题将我带到此处发布的答案

最佳答案

其实他们并不相等。因为任何 Rentable-Instance 都允许给定任何类型参数 T,而 OutsourcableDeveloper 限制它。

当然你可以假设在你的情况下很容易使用

<C> Rate<C> getRate(Taxes<C> taxes);  

接口(interface)的版本。但是,如果开发人员想要子类化 OutsourceableDeveloper,他会很困惑。根据 Developer 的定义,他可以假设方法 getRate 固定为 C 但实际上它可以突然取任何值。 -> 允许这样做会导致混淆。

我可以为您提供以下代码示例,它可能适合您的情况。虽然用起来肯定不方便。但是当您将所有方法转发给 OursourcableDeveloperRentable 时,这是可能的。评论应该解释它是如何工作的。

//This class itself can be added to any Developer-lists
public class OutsourcableDeveloper<C> extends Developer<C> {

    public final OutSourcableDeveloperRentable RENTABLE_INSTANCE = new OutSourcableDeveloperRentable();

    @Override
    public Rate<C> getRate(final Taxes<C> taxes) {
        // Simply forward to the more general getRate instance.
        return this.RENTABLE_INSTANCE.getRate(taxes);
    }

    public void exampleBehaviourA() {
        //Example for how you can make both objects behave equally.
    }

    // This class can be added to the lists requiring a Rentable
    // And the original value can be retrieved by both classes.
    public class OutSourcableDeveloperRentable implements Rentable {

        public final OutsourcableDeveloper<C> PARENT_INSTANCE = OutsourcableDeveloper.this;

        //This method is the one to implement because it is more general than
        //the version of OutsourcableDeveloper.
        @Override
        public <T> Rate<T> getRate(final Taxes<T> taxes) {
            // Do your work.
            return null;
        }

        public void exampleBehaviourA() {
            //Just an example - Maybe for you it makes for sence to
            //forward the method of Oursoursable-Developer to here.
            //Then all Behaviour would be found in this class.
            OutsourcableDeveloper.this.exampleBehaviourA();
        }

    }
}

关于java - Name Clash, override fail, on a class 实现两个具有相同删除的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34242934/

相关文章:

java - 使用 Hibernate 无法持久化一对多层次结构对象结构

java - iReport/jFreeChart - 结合 getItemPaint() 覆盖和系列颜色

c# - 复杂的类声明混淆了表单设计器?

java - 在调用父类(super class)的构造函数之前解析子类中的参数

xcode - 在 Xcode 中使用 swift 更改子类中父类(super class)中的变量

java - 指定多个类修饰符

java - eclipse 中的cobertura

java - 将泛型类型对象写入 java 文件

java - 使自定义控件通用

c++ - 比较来自两个不同类的对象?