java 接口 方法的 不同_Java – 使用相同的方法和不同的返回类型实现多个接口...

请考虑以下代码:

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的合同要求外,还有其他解决方案/解决方法吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值