我知道這是不可能的: new T
其中T
是一種通用類型,由於類型擦除。爲什麼我們可以在Java中創建通用模板的新實例而不是泛型?
爲什麼然後可以做類似new A<T>
這樣的T是通用的?例如,爲什麼以下編譯?
static class A<T>{
T t;
A(T t){
this.t=t;
}
}
static class B<T>{
A<T> create(T t){
return new A<T>(t);
}
}
public static void main (String[] args) throws java.lang.Exception
{
A<Integer> a = new B<Integer>().create(3);
}