我们已经开始收到编译所使用泛型的代码错误和Java的6下成功编译Java的泛型编译时错误这里有一个简单的类重现:迁移从Java 6到7或8
class Test {
static class Foo<T> {
T t;
Foo(T t) { this.t = t; }
T get() { return t; }
}
static class Bar extends Foo<Long> {
Bar(Long t) { super(t); }
}
static class Foobar<N extends Number> extends Bar {
Foobar() { super(5L); }
}
public static void main(String[] args) {
Bar bar = new Bar(0L);
Long b = bar.get(); // This works
Foobar foobar = new Foobar();
Long fb = foobar.get(); // This generates a compile time error
}
}
由此产生的错误是:
Test.java:26: error: incompatible types: Object cannot be converted to Long
Long fb = foobar.get(); // This generates a compile time error
有没有人有任何想法?
对不起,没有提到编译错误发生在Java 7和8中,但不是在6 ... –
奇怪的是,虽然我从命令行使用jdk1.7.0_13得到这个编译错误,但这段代码编译得很好在eclipse中,使用相同的编译器(双重检查“已安装的JRE”指向相同的路径,合规性级别为1.7,并使用该jdk)。 – azurefrog
我得到了完全一样的 - Eclipse编译1.8集没有错误。很奇怪...... –