原文地址
http://zhidao.baidu.com/question/168167280.html
package test.wendellup;
public class Student {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
package test.wendellup;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class GenericDAO<T> {
public Class<T> entityClass;
protected GenericDAO() {
Type type = getClass().getGenericSuperclass();
System.out.println(getClass().getName());
Type trueType = ((ParameterizedType) type).getActualTypeArguments()[0];
this.entityClass = (Class<T>) trueType;
}
}
package test.wendellup;
public class OptionManager extends GenericDAO<Student> {
public static void main(String[] args) throws Exception {
OptionManager manager = new OptionManager();
System.out.println(manager.entityClass.getName());
}
}