参数化类型

参考 https://www.jianshu.com/p/e8eeff12c306

package com.software.paramstype;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class TestParams3 {
    public static void main(String[] args) {
        System.out.println(Person.class.getGenericSuperclass());
        System.out.println("Student.class.getSuperclass()\t"
                + Student.class.getSuperclass());//返回直接继承的父类(由于编译擦除,没有显示泛型参数)
        System.out.println("Student.class.getGenericSuperclass()\t"
                + Student.class.getGenericSuperclass());//返回直接继承的父类(包含泛型参数)

        System.out.println("Test.class.getSuperclass()\t"
                + TestParams3.class.getSuperclass());
        System.out.println("Test.class.getGenericSuperclass()\t"
                + TestParams3.class.getGenericSuperclass());

        System.out.println("Object.class.getGenericSuperclass()\t"
                + Object.class.getGenericSuperclass());
        System.out.println("Object.class.getSuperclass()\t"
                + Object.class.getSuperclass());

        System.out.println("void.class.getSuperclass()\t"
                + void.class.getSuperclass());
        System.out.println("void.class.getGenericSuperclass()\t"
                + void.class.getGenericSuperclass());

        System.out.println("int[].class.getSuperclass()\t"
                + int[].class.getSuperclass());
        System.out.println("int[].class.getGenericSuperclass()\t"
                + int[].class.getGenericSuperclass());
        Type genType = Student.class.getGenericSuperclass();
        Type type = ((ParameterizedType) genType).getActualTypeArguments()[0];
        if (type instanceof ParameterizedType) {
            //获取参数类型泛型中Dog<T> T类型值
            System.out.println(((ParameterizedType) type).getActualTypeArguments()[0]);
            //返回参数类型泛型中<>前面的那个值
            //com.software.paramstype.TestParams3$Dog
            System.out.println(((ParameterizedType) type).getRawType());
        } else if (type instanceof Class) {
            System.out.println("type===>class");
        } else {
            System.out.println("type===>else");
        }
    }

    class Dog<T> {

    }
    class Student extends Person<Dog<TestParams3>> {

    }
}
///log 
Student.class.getSuperclass()	class com.software.paramstype.Person
Student.class.getGenericSuperclass()	com.software.paramstype.Person<com.software.paramstype.TestParams3$Dog<com.software.paramstype.TestParams3>>
Test.class.getSuperclass()	class java.lang.Object
Test.class.getGenericSuperclass()	class java.lang.Object
Object.class.getGenericSuperclass()	null
Object.class.getSuperclass()	null
void.class.getSuperclass()	null
void.class.getGenericSuperclass()	null
int[].class.getSuperclass()	class java.lang.Object
int[].class.getGenericSuperclass()	class java.lang.Object
class com.software.paramstype.TestParams3
class com.software.paramstype.TestParams3$Dog

Process finished with exit code 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值