个人理解:这两个方法就是得到超类的泛型类型 和 接口的泛型类型
代码:
1 基础类:
package test2; public class AccessibleTest extends Person<Test> implements SuperPerson<Person>{ private int id=1; private String name="网"; public AccessibleTest(){ System.out.println( "accesible Test"); } public int getId() { return id; } public String getName() { return name; } @Override public void saySuperPerson(Person t) { // System.out.println( t ); } }
2 超类
package test2; public class Person<T> { public Person(){ System.out.println( getClass()+"======Person==" ); } private String name="wangtony" ; private Integer age =12; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
3 接口
package test2; public interface SuperPerson<T> { public void saySuperPerson(T t); }
4 测试类
package test2; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; public class Test2 { public static void main(String[] args) throws Exception { // Entity[] e = new Entity[2]; // e[0] = new Entity(); // int[] i = {1,3}; Class clazz = Class.forName("test2.AccessibleTest"); AccessibleTest a = new AccessibleTest(); clazz = a.getClass(); Type genType = clazz.getGenericSuperclass(); //得到这个类的父类 的参数类型 if (genType instanceof ParameterizedType) { //如果是 泛型的参数类型 ,就得到泛型类型 Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); //强转换为参数类型 for(Type t : params ){ System.out.println( t ); } System.out.println( "=========="); } //得到接口的泛型类型 Type[] type1 = clazz.getGenericInterfaces(); if( type1!=null && type1.length>0 ){ for(int i=0;i<type1.length;i++ ){ //循环泛型类型 //如果得到的泛型类型是 ParameterizedType(参数化类型)类型的话 if(ParameterizedType.class.isAssignableFrom( type1[i].getClass() ) ){ ParameterizedType ptype = (ParameterizedType)type1[i];//将类型转换为 参数类型类 Type[] iType = ptype.getActualTypeArguments(); //得到 泛型类型的实际 类型 for(Type p1 : iType){ System.out.println(p1 +"=======" ); } } } } } }