Pau*_*ton 28 java java-8 default-method
假设有两个接口Interface1,Interface2其中Interface2扩展Interface1.
interface Interface1 {
default void method() {
System.out.println("1");
}
// Other methods
}
interface Interface2 extends Interface1 {
@Override
default void method() {
System.out.println("2");
}
// Other methods
}
Run Code Online (Sandbox Code Playgroud)
假设我想创建一个实现的类,Interface2但我想method()成为其中的版本Interface1.如果我写
class MyClass implements Interface1, Interface2 {
public void method() {
Interface1.super.method();
}
}
Run Code Online (Sandbox Code Playgroud)
我收到编译错误:
默认超级调用中的错误类型限定符:冗余接口Interface1由Interface2扩展
可以通过创建第三个界面来解决这个问题:
interface Interface3 extends Interface1 {
default void method() {
Interface1.super.method();
}
}
Run Code Online (Sandbox Code Playgroud)
然后:
class MyClass implements Interface1, Interface2, Interface3 {
public void method() {
Interface3.super.method();
}
}
Run Code Online (Sandbox Code Playgroud)
这编译很好,如果我实例化一个新的MyClass和调用method(),输出是1预期的.
所以我的问题是,考虑到你可以只InterfaceName.super.method()针对链中最具体的界面编写限制,很容易绕过限制,限制的原因是什么?Interface1.super.method()首先禁止你写作会阻止哪些问题?
Rad*_*def 18
这正是JLS在15.12.3中 解决的问题."编译 - 时间步骤3:选择的方法是否合适?" .
如果表单是TypeName.超级 [TypeArguments]标识符,然后:
JLS继续解释为什么规则到位:
在超级接口覆盖祖父节点接口中声明的方法的情况下,此规则通过简单地将祖父节点添加到其直接超接口列表中来防止子接口"跳过"覆盖.访问祖父母的功能的适当方式是通过直接超级接口,并且仅当该接口选择暴露期望的行为时.
因此,或多或少存在专门阻止你做你想做的事情.
但JLS似乎也承认你的解决方法:
(或者,开发人员可以自由定义自己的附加超接口,通过超级方法调用公开所需的行为.)