获取范型参数

2018-06-04  本文已影响4人  oneWeekOneTopic

1. field

public class GenericTypeTest {

public static void main(String[]arg)throws NoSuchFieldException,SecurityException{

Field f=Pojo.class.getField("list");//反射获得泛型成员变量

        System.out.println(f.getGenericType());//输出成员变量类型

        Type[] type=((ParameterizedType)f.getGenericType()).getActualTypeArguments();//获得泛型的所有泛型参数

        for(Type t:type){

System.out.println(t);//遍历输出各个参数

        }

}

public static class Pojo{

public Listlist;//泛型成员变量

    }

}

2. 超类

上一篇 下一篇

猜你喜欢

热点阅读