在java程序运行时,读取某个类的泛型实参 步骤: 1、获取Class对象 2、获取泛型父类 3、获取泛型实参数 示例: class Father<T,U>{ class Son extends Father<Integer, String>{ //读取泛型实参 @Test public void test01(){ Class clazz = Son.class; ParameterizedType type = (ParameterizedType) clazz.getGenericSuperclass(); Type[] types = type.getActualTypeArguments(); for (Type t: types){ System.out.prinln(t); } }
|
|