【发布时间】:2019-02-28 01:53:02
【问题描述】:
考虑这个 Java 程序:
public class IntersectionBug {
public static interface FooInterface {
public void foo();
}
public static class FooSupport {
protected void foo() {
}
}
public static class Whatever<T extends FooSupport & FooInterface> {
}
}
在JDK 1.8编译器下编译失败:
$ javac IntersectionBug.java
IntersectionBug.java:12: error: foo() in FooSupport cannot implement foo() in FooInterface
public static class Whatever<T extends FooSupport & FooInterface> {
^
attempting to assign weaker access privileges; was public
1 error
显然,如果某个类型 T 既是 FooSupport 又是 FooInterface,那么它必须有 public void foo(),所以错误是假的。
我的问题:这是编译器错误,还是 JLS 真的指定该程序无效?如果是后者,为什么这里的 JLS 行为是次优的?
【问题讨论】: