我剛剛開始學習關於Generic.Here我試圖設置全局obj的值爲本地obj的值。但我得到了類型轉換error.What是這個錯誤的原因是什麼?類型轉換錯誤通用
class GenUpperBound<T>
{
T obj;
public <T extends Number> void set(T obj)
{
this.obj=obj;
}
public static void main(String...q)
{
GenUpperBound<Integer> w=new GenUpperBound<>();
w.set(10);
}
}
以下是錯誤....
GenupperBound.java:6: error: incompatible types: T#1 cannot be converted to T#2
this.obj=obj;
^
where T#1,T#2 are type-variables:
T#1 extends Number declared in method <T#1>set(T#1)
T#2 extends Object declared in class GenUpperBound
1 error