public class Women extends Person<Women> { private static final String TAG = "Women"; public void execute() { Type type = this.getClass().getGenericSuperclass(); Log.e(TAG, "type.." + type); Type[] types = ((ParameterizedType) type).getActualTypeArguments(); Log.e(TAG, "size.." + types.length); for (int i = 0; i < types.length; i++) { Log.e(TAG, types[i].toString()); } } }
women为person的子类,当我们调用执行时
Type type = this.getClass().getGenericSuperclass();
(1)看到其中的superclass(),可以看出得到的type是其父类的类型,与getsuperclass的区别是可以获得带有泛型的父类,而getsuperclass只能获取父类,没有泛型
Women: type..com.example.administrator.Bean.Person<com.example.administrator.Bean.Women>
其中的泛型为women
(2)ParameterizedType是Type的子接口。用于获取java泛型参数类型
实质上types长度就是当前泛型类型的个数,目前也就是1
Women: size..1
(3)通过types[0]取出的就是当前的泛型类。可将其转化为class类
Women: class com.example.administrator.Bean.Women