获取泛型类型学习笔记

  1. 编写类继承TypeReference
class AAA<T> extends TypeReference<T> {
    
    
    public void hello(){
    
    
        Type rawType = this.getRawType();
        System.out.println(rawType.getTypeName());
    }
}
  1. 编写具体类指定具体泛型
class BBB extends AAA<Person>{
    
    

}
class Person{
    
    }
  1. 编写代码测试代码
public class TypeReferenceTest {
    
    
    @Test
    public void test1(){
    
    
        new BBB().hello();
    }
}
//打印出泛型的实际类型为Person
//com.yicj.study.type.TypeReferenceTest$Person

猜你喜欢

转载自blog.csdn.net/yichengjie_c/article/details/109927460
今日推荐