【发布时间】:2014-10-03 21:01:48
【问题描述】:
运行 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;
}
}
给出编译错误:
SimpleQuestion类型中的DoNothing()方法不适用于参数()
为什么是那个而不是 DoNothing2、3 和 4?
【问题讨论】:
-
在 Eclipse 中失败。 Oracle 编译器为我工作。我哭了!
-
但是,如果您明确提供类型参数,它就可以工作。
-
在 Java7 中编译得很好;可能是您正在使用的编译器/IDE 中的错误
-
为什么所有
void方法都有return ;语句?此外,DoNothing2()方法会导致编译器错误(untested 但基于我所看到的,因为Interface2还没有有界)。 -
我不知道你在说什么,它在 java8 和 java7 中都适用于我。您的软件可能存在问题,或者您可能犯了其他错误。
标签: java eclipse generics java-8 multiple-inheritance