public class DAO<T> {
private Class<T> clazz;
public DAO(){
//获取DAO 子类的带泛型参数的父类
Type type= this.getClass().getGenericSuperclass();
System.out.println(type);
//获取具体的泛型参数
if(type instanceof ParameterizedType){
ParameterizedType parameterizedType = (ParameterizedType) type;
Type[] args = parameterizedType.getActualTypeArguments();
System.out.println(Arrays.asList(args));
if(args != null && args.length>0){
Type arg = args[0];
System.out.println(arg);
if(arg instanceof Class){
clazz = (Class<T>) arg;
System.out.println(clazz);
}
}
}
}
}
<strong><span style="color:#33cc00;">superClass</span><span style="color:#33ff33;">:</span></strong> com.baidu.app.dao.DAO<com.baidu.app.domain.Customer>
<strong><span style="color:#33cc00;">parameterizedType</span></strong>: com.baidu.app.dao.DAO<com.baidu.app.domain.Customer>
<strong><span style="color:#33cc00;">args.length</span></strong>: 1
<strong><span style="color:#33cc00;">args[0]</span></strong>: class com.baidu.app.domain.Customer