//泛型基类
public class Base <T>{
private Class clazz;
public Base(){
try {
System.out.println("Base 构造函数");
Type type=(Type) this.getClass().getGenericSuperclass();
//调试知 type = com.yq.GenericType.Base<com.yq.GenericType.GenericType>
type的实际类型是ParameterizedType
ParameterizedType pt=(ParameterizedType) type;
this.clazz=(Class) pt.getActualTypeArguments()[0];
//上面的函数的返回的类型是Type 而实际类型是Class
clazz.newInstance();//实例化
} catch (Exception e) {
// TODO: handle exception
}
}
}
//子类
class Sub extends Base<GenericType>{
public Sub(){
System.out.println("Sub 构造函数 ");
}
}
//domain
class GenericType{
public GenericType(){
System.out.println("Domain ... GenericType构造函数...");
}
}
泛型参数化在实际项目中的应用 就是GenericDao GenericAction 动态获取domain类型