考虑下面的例子,
public class Testing extends SupCls implements Intf {
public static void main(String[] args) {
new Testing().test();
}
}
class SupCls {
public void test() {
System.out.println("From SupCls");
}
}
interface Intf {
public default void test() {
System.out.println("From Intf");
}
}
正如你所看到的,两者之间没有任何联系SupCls
类和Intf
界面。但两者都是defining一个常用的方法。
And Testing
班级正在扩大SupCls
并实施Intf
.
所以,当我打电话时test()
方法上Testing
输出是
From SupCls
我认为这是有道理的,因为从类扩展应该比从接口实现具有更高的优先级。
但 eclipse 却报告了其他情况,如下面的屏幕截图所示。
我坚信这是一个Eclipse 中的错误.
但在假设之前,这种行为是否已定义并记录在JLS?或者还有其他什么东西可以定义这种行为?
编辑:Eclipse 版本是火星发布 (4.5.0),如果重要的话。
你的假设是正确的,从超类继承的具体方法优先于default
方法从interface
:
JLS §8.4.8。继承、覆盖和隐藏 https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.8
A class C
inherits来自其直接超类和直接超级接口abstract
和默认(§9.4)方法m
以下所有条件均成立:
…
- 没有声明方法
C
签名是以下签名的子签名(第 8.4.2 节)m
.
- 没有继承的具体方法
C
来自其直接超类的签名是以下签名的子签名m
.
第二个引用的项目符号适用于此,有一个从直接超类继承的具有适当签名的具体方法,因此default
方法不被继承。
该文档甚至通过附加注释消除了任何疑问:
请注意,继承的具体方法可能会阻止抽象或默认方法的继承。 (稍后我们将断言具体方法会覆盖“来自 C”的抽象或默认方法。)
所以就像SupCls.test()
覆盖Intf.test()
当谈到上课时Testing
.
In other words, you are right, it’s a bug in Eclipse, but as long as it only affects the way the proposal is rendered, I’d consider it a minor bug. The inserted source will be the same, regardless of whether a D has been rendered in the proposal or not.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)