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