Java 中泛型如何将泛型实例化?

1
2
3
4
5
6
7
8
9
10
11
public static<T> List<T> getRecords(Class<T> c, Selector s) {
// Use Selector to select rows
List<T> list = new ArrayList<T>();
for (/* iterate over results */) {
T row = c.newInstance(); // use reflection to set fields from result
list.add(row);
}
return list;
}
List<FooRecord> l = getRecords(FooRecord.class, fooSelector);

此文在我的 Github Pages 上同步发布,地址为:Java泛型