stackoverflow上 也有人这么写
https://stackoverflow.com/questions/49939675/how-to-create-new-object-of-generic-type-t-from-a-parametrized-listt
How to create new object of Generic Type T from a parametrized List<T>
public class TestClass {
public static <T> void method(List<T> objects) throws Exception {
for (int i = 0; i < objects.size(); i++) {
// Create new object of the same class
T obj = (T) objects.get(i).getClass().newInstance();
}
}
}
--
修改:tortelee FROM 223.166.166.*
FROM 223.166.166.*