我有一个不可变的类,具有以下布局,
public final class A <T> {
private ArrayList<T> myList;
private A(){
myList = new ArrayList<T>();
}
public A<T> addData(T t){
A newOb = // make a new list and instantiate using overloaded constructor
T newT = new T(); ***********ERROR HERE*************
newOb.myList.add(newT);
return newOb;
}
.........
}
我在这里得到的错误是cannot instantiate type T
。现在,我认为这可能与type erasure
在爪哇。
我怎样才能克服这个问题?我想将传递给 addData 的参数的新副本添加到我的列表中。
T newT = (T) t.getClass().newInstance() // assuming zero args constructor and you'll
// have to catch some reflection exceptions
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)