考慮下面的代碼:爲什麼超類通用型不擦除/鑄造
class A<T>{
T t;
public T getValue(){
return t;
}
class B<S extends Number> extends A<String>{
//some code here..
}
B b = new B<Integer>();
String name = b.getValue() // This throws compilation error
而以下工作:
B<Integer> b = new B<Integer>();
String name = b.getValue() // This works...!
所以我的問題是:
我是否必須聲明所有涉及的泛型類型? (即使A類的通用類型在B類中聲明)
或者,我是否缺少基本的東西?
你忽視了關鍵的部分,它是)B.getValue的'的聲明('。 –
之間,爲什麼我應該重寫B中的方法,當我不想更改getValue()的任何行爲?此外,A的類型已被聲明爲B中的Long,以將其刪除。 –
@JimGarrison但'getValue'的縮寫在類'A' – SomeJavaGuy