请考虑以下代码:
public interface A {
public A another();
}
public interface B {
public B another();
}
public interface AB extends A,B {
public AB another();
}
这导致AB上的编译错误:
types B and A are incompatible; both define another(), but with
unrelated return types
我已经看过这个SO question,并按照接受的答案中的不兼容性示例 – 即
public interface C {
public void doSomething();
}
public interface D {
public boolean doSomething();
}
public interface CD extends C,D {
}
但是,在这种情况下,返回类型实际上是不兼容的 – 返回类型不能同时为void和布尔值.然而,在上面的示例中,AB的another()返回类型既是A又是B,因此可以实现两个扩展接口.
此外,看过JLS(8.4.8,8.4.8.3,8.4.8.4),我不太明白为什么我的上面的例子非法.任何人都可以向我解释这个吗?
其次,除了重复AB中A或B的合同要求外,还有其他解决方案/解决方法吗?