Class类的getGenericSuperClass

来源:互联网 发布:诺基亚软件下载官网 编辑:程序博客网 时间:2024/05/16 05:43
    Returns the Type representing the direct superclass of the entity (class, interface, primitive type or void) represented by
    thisClass.

    If the superclass is a parameterized type, the Type object returned must accurately reflect the actual type parameters used
     in the source code. The parameterized type representing the 
    superclass is created if it had not been created before. See the
     declaration of ParameterizedType for the semantics of the 
    creation process for parameterized types. If thisClass represents either theObject class, an interface, a primitive type, or void, then null is returned. If this object represents an array class then theClass object representing theObject class is returned

    概括来说就是对于带有泛型的class,返回一个ParameterizedType对象,对于
    Object、接口和原始类型返回null,对于数 组class则是返回Object.class。
    ParameterizedType是表示带有泛型参数的类型的Java类型,JDK1.5引入了泛
    型之 后,Java中所有的Class都实现了Type接口,ParameterizedType则是继
    承了Type接口,所有包含泛型的Class类都会实现 这个接口。

    实际运用中还要考虑比较多的情况,比如获得泛型参数的个数避免数组越界等,具
    体可以参看Gson中的TypeToken类及ParameterizedTypeImpl类的代码。
1 0
原创粉丝点击