一:泛型方法:
1、
2、
1、
private void test1(T t){
t.toString();
}
与
2、
private <T> void test1(T t){
t.toString();
}
的区别是方法所属的类是否进行了泛型声明。
如果类是public class ClassName<T>则可用第一种,如果类没有<T>的话则第一种不能通过编译。
public static <T> Class<T> getGenericClass(final Class baseClass,
final int index) {
Type genericType = baseClass.getGenericSuperclass();
if (genericType instanceof ParameterizedType) {
Type[] params = ((ParameterizedType) genericType)
.getActualTypeArguments();
if (null != params && params.length >= index) {
if (params[index - 1] instanceof Class)
return (Class<T>) params[index - 1];
}
}
return null;
}