getSuperclass 与 getGenericSuperclass 如果继承的父类没有泛型,那么返回的结果是相同的,如果继承的父类包含泛型,则会返回带泛型的父类类型(sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class A<T> { }

class B extends A { }

class C extends A<String> { }

public class Main {
    public static void main(String[] args) {
        System.out.println(B.class.getSuperclass());        // class me.yuanzx.test.A
        System.out.println(B.class.getGenericSuperclass()); // class me.yuanzx.test.A
        System.out.println(C.class.getSuperclass());        // class me.yuanzx.test.A
        // 打印出带泛型的类型
        System.out.println(C.class.getGenericSuperclass()); // me.yuanzx.test.A<java.lang.String>
    }
}