java - 具有不相关返回类型的继承方法

标签 java inheritance method-hiding

我有以下代码片段

public class Test {
    static interface I1 { I1 m(); }

    static interface I2 { I2 m(); }

    static interface I12 extends I1,I2 { I12 m(); }

    public static void main(String [] args) throws Exception {
    }
}

当我尝试编译它时出现错误。

Test.java:12: types Test.I2 and Test.I1 are incompatible; both define m(), but with unrelated return types.

如何避免这种情况?

最佳答案

Java - Method name collision in interface implementation 中所述你不能这样做。

作为解决方法,您可以创建一个适配器类。

关于java - 具有不相关返回类型的继承方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9704219/

相关文章:

objective-c - 从 GCD dispatch_async block 中调用 super : is it safe?

java - 在 Java 中使用泛型返回接口(interface)实例的枚举?

android - 继承和泛型 java android

ruby-on-rails - ActiveRecord多级单表继承查询

c# - 抽象方法和隐藏方法

c# - 方法隐藏在 C# 中如何工作? (第二部分)

java - 未提交的消息不会再次被消耗

java - 初始循环后循环打印输出两次

java - 带有内部类QueueNode的LinkedList队列推送方法

c# - 为什么继承不像我认为的那样起作用?