use*_*552 8 java eclipse generics multiple-inheritance java-8
运行Java 1.8 JavaSE-1.8(jdk1.8.0_20)
这个班:
public class SimpleQuestion {
public static void main(String[] args) {
DoNothing();
DoNothing2();
DoNothing3();
DoNothing4();
}
public interface Interface1 {
public void go();
}
public interface Interface2<X> {
public X go2();
}
private static <X, T extends Interface2<X> & Interface1> void DoNothing() {
return;
}
private static <X, T extends Interface2 & Interface1> void DoNothing2() {
return;
}
private static <X, T extends Interface2<X>> void DoNothing3() {
return;
}
private static <T extends Interface2<T> & Interface1> void DoNothing4() {
return;
}
}
Run Code Online (Sandbox Code Playgroud)
给出编译错误:
SimpleQuestion类型中的方法DoNothing()不适用于参数()
为什么那个而不是DoNothing2,3和4?
该错误消息似乎是指规范第 18.5.1 节中定义的算法失败。
对于 DoNothing,算法按如下方式进行(使用上述链接中的术语):
类型参数是
P1 = X
P2 = T extends Interface2<X> & Interface1
我将使用 a1 和 a2 作为相应的推理变量。
初始边界集是
B0 = {a1 <: Object, a2 <: Interface2<a1>, a2 <: Interface1}
没有参数,因此此时没有添加额外的边界 (B2 = B0)。
a2对a1有依赖关系,所以我们尝试先解析a1。它有一个适当的 Object 上限,因此我们将其实例化为此。合并a1 = Object涉及添加边界
a2 <: Interface2<Object>
接下来我们解决a2。现在它有两个正确的上限,因此我们将 a2 实例化为它们的 glb:
a2 = Interface2<Object> & Interface1
现在每个变量都有一个实例化,因此解析已成功。
因此,与错误消息相反,DoNothing 的调用应该适用。这似乎是 Java 编译器中的一个错误。
| 归档时间: |
|
| 查看次数: |
308 次 |
| 最近记录: |