java泛型父类获取子类传递泛型

abstract class BaseDao {
    public BaseDao() {
        Type t = getClass().getGenericSuperclass();
        ParameterizedType p = (ParameterizedType) t ;
        Class c = (Class) p.getActualTypeArguments()[0];
        System.out.println("子类通过junity来实例化,会先实例化父类:"+c.getName());    
    }
}
public class SubDao extends BaseDao {
    public SubDao() {
        System.out.println("子类构造");
    }

    @Test
    public void test1() {

    }
}

java泛型父类获取子类传递泛型_第1张图片

你可能感兴趣的:(java)