java泛型的强大功能

public static <T> List<T> parseList(String xlsName, String sheetName);

使用时不需要指定泛型类型,参考自guva。

public <T> T newInstantce() {
Type genType = getClass().getGenericSuperclass();
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();

Class<T> clazz = (Class) params[0];
T obj = null;
try {
obj = clazz.newInstance();
} catch (Exception e) {
e.printStackTrace();
log.error(“ERROR”, e);
}
return obj;
}

生成泛型的实例。

发表评论?

0 条评论。

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>