【发布时间】:2020-05-22 23:56:18
【问题描述】:
以下代码编译良好:
public class Test {
public static interface A<Y> {
public B<Y> foo();
}
public static interface B<Y> extends A<Y> {
public A<Y> bar();
}
private static class Impl
implements A, B
{
public B foo() {
return this;
}
public A bar() {
return null;
}
}
}
但是当一个泛型参数被引入顶级类Test时,代码不再编译:
public class Test<X> {
public static interface A<Y> {
public B<Y> foo();
}
public static interface B<Y> extends A<Y> {
public A<Y> bar();
}
private static class Impl
implements A, B
{
public B foo() {
return this;
}
public A bar() {
return null;
}
}
}
我得到的错误是:
接口 A 不能使用不同的参数多次实现:Test.A 和 Test.A
是什么导致了这个奇怪的编译错误?有什么办法可以解决?
顺便说一下,我用的是eclipse和Java 1.8.0_144。
感谢您的帮助。
【问题讨论】:
-
我无法重现错误,而且您的 Impl 不应该使用原始类型,您需要指定 Y,例如
implements A<Integer>, B<Integer> -
@azro 您使用的是什么版本的 Java?你使用的是什么编译器实现?
-
Nit:接口总是静态的,不需要显式声明它们是静态的。
-
@Andy Turner 很可能是;这段代码在当前的迭代中肯定不是很漂亮,甚至不太明智,因为我已经摆弄它很长一段时间了,试图找出它为什么不能编译。
-
它是否与
javac一起编译? Eclipse 有自己的编译器(或者至少曾经有)。
标签: java generics compiler-errors inner-classes