我們已經開始收到編譯所使用泛型的代碼錯誤和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集沒有錯誤。很奇怪...... –