父类代码:
package cn.generic;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class Base<T,PK> {
protected Class<T> entityClass;
protected Class<PK> pkClass;
public Base() {
Type genType = getClass().getGenericSuperclass();
Type[] params = ((ParameterizedType)genType).getActualTypeArguments();
this.entityClass = (Class)params[0];
this.pkClass = (Class)params[1];
}
}
子类及测试泛型代码:
package cn.generic;
public class Impl extends Base<String, Long> {
public static void main(String[] args) {
Impl impl = new Impl();
System.out.println(impl.entityClass); // class java.lang.String
System.out.println(impl.pkClass); // class java.lang.Long
}
}