讓我們考慮下面的代碼:Java中的getGenericSuperclass()。它是如何工作的?
package index;
public class Main {
public static void main(String[] args) {
Inner inner = new Inner();
Type t = inner.getClass().getGenericSuperclass();
ParameterizedType p = (ParameterizedType) t;
Type[] a = p.getActualTypeArguments();
try {
Custom c = (Custom) ((Class) a[0]).newInstance();
c.f();
} catch (Exception e){}
}
private static abstract class AbstractClass<T> {
public abstract void doSth();
}
private static class Inner extends AbstractClass<Custom>{
public void doSth() {
}
}
private static class Custom{
public Custom(){
}
public void f(){
System.out.println("Custom");
}
}
}
它的工作原理,我不明白它是如何做的工作。它表明可以獲取有關超類參數的信息。我不知道如何獲得這樣的信息,因爲我查看字節碼,並且沒有這樣的信息:
可以這樣做嗎?
1.閱讀['Class.getGenericSuperclass()'](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getGenericSuperclass--)javadoc。是的。 –
你顯示的是'Main',而不是'Inner'的字節碼,所以它不能告訴你,Inner是否存儲了它的通用超類。準確地說,這甚至不是字節碼,這是某個字節碼處理程序的輸出。如果該程序輸出缺少某些信息,則不能證明該信息在類文件中不存在。 – Holger