一篇文章搞定java的泛型

我就是我 2023-02-16 04:13 81阅读 0赞

一、泛型的定义和意义

1. 定义

泛型,即“参数化类型”。就是将类型由原来的具体的类型参数化,类似于方法中的变量参数,此时类型也定义成参数形式(可以称之为类型形参),然后在使用/调用时传入具体的类型(类型实参)

2. 泛型的意义

  1. 适用于多种数据类型执行相同的代码(代码复用)
  2. 泛型中的类型在使用时指定,不需要强制类型转换(类型安全,编译器会检查类型)

3. 泛型的特性

泛型只在编译阶段有效

  1. @Test
  2. public void test1(){
  3. List<String> stringArrayList = new ArrayList<String>();
  4. List<Integer> integerArrayList = new ArrayList<Integer>();
  5. Class classStringArrayList = stringArrayList.getClass();
  6. Class classIntegerArrayList = integerArrayList.getClass();
  7. System.out.println(classIntegerArrayList);
  8. System.out.println(classStringArrayList);
  9. System.out.println(classStringArrayList.equals(classIntegerArrayList));
  10. }

在这里插入图片描述
通过上面的例子可以证明,在编译之后程序会采取去泛型化的措施。也就是说Java中的泛型,只在编译阶段有效。在编译过程中,正确检验泛型结果后,会将泛型的相关信息擦出(泛型擦除/类型擦除),并且在对象进入和离开方法的边界处添加类型检查和类型转换的方法。也就是说,泛型信息不会进入到运行时阶段。

对此总结成一句话:泛型类型在逻辑上看以看成是多个不同的类型,实际上都是相同的基本类型。

二、泛型的使用

1. 一些常用的泛型类型变量

E:元素(Element),多用于java集合框架
K:关键字(Key)
N:数字(Number)
T:类型(Type)
V:值(Value)

2. 泛型的约束

  1. 不能实例化泛型类型变量
  2. 静态变量或方法不能引用泛型类型变量,但是静态泛型方法是可以的
  3. 基本类型无法作为泛型类型
  4. 无法使用instanceof关键字或==判断泛型类的类型
  5. 泛型类的原生类型与所传递的泛型无关,无论传递什么类型,原生类是一样的
  6. 泛型数组可以声明但无法实例化
  7. 泛型类不能继承Exception或者Throwable
  8. 不能捕获泛型类型限定的异常但可以将泛型限定的异常抛出

    /*

    Description: 泛型的约束和局限性 /
    public class GenericRestrict1 {

    1. static class NormalClass {
    2. }
    3. private T data;
    4. /** * 不能实例化泛型类 * Type parameter 'T' cannot be instantiated directly */
    5. public void setData() {
    6. //this.data = new T();
    7. }
    8. /** * 静态变量或方法不能引用泛型类型变量 * 'com.jay.java.泛型.restrict.GenericRestrict1.this' cannot be referenced from a static context */

    // private static T result;

    // private static T getResult() {
    // return result;
    // }

    1. /** * 静态泛型方法是可以的 */
    2. private static <K> K getKey(K k) {
    3. return k;
    4. }
    5. public static void main(String[] args) {
    6. NormalClass normalClassA = new NormalClass();
    7. NormalClass normalClassB = new NormalClass();
    8. /** * 基本类型无法作为泛型类型 */

    // GenericRestrict1 genericRestrictInt = new GenericRestrict1<>();

    1. GenericRestrict1<Integer> genericRestrictInteger = new GenericRestrict1<>();
    2. GenericRestrict1<String> genericRestrictString = new GenericRestrict1<>();
    3. /** * 无法使用instanceof关键字判断泛型类的类型 * Illegal generic type for instanceof */

    // if(genericRestrictInteger instanceof GenericRestrict1){
    // return;
    // }

    1. /** * 无法使用“==”判断两个泛型类的实例 * Operator '==' cannot be applied to this two instance */

    // if (genericRestrictInteger == genericRestrictString) {
    // return;
    // }

    1. /** * 泛型类的原生类型与所传递的泛型无关,无论传递什么类型,原生类是一样的 */
    2. System.out.println(normalClassA == normalClassB);//false
    3. System.out.println(genericRestrictInteger == genericRestrictInteger);//
    4. System.out.println(genericRestrictInteger.getClass() == genericRestrictString.getClass()); //true
    5. System.out.println(genericRestrictInteger.getClass());//com.jay.java.泛型.restrict.GenericRestrict1
    6. System.out.println(genericRestrictString.getClass());//com.jay.java.泛型.restrict.GenericRestrict1
    7. /** * 泛型数组可以声明但无法实例化 * Generic array creation */
    8. GenericRestrict1<String>[] genericRestrict1s;

    // genericRestrict1s = new GenericRestrict1[10];

    1. genericRestrict1s = new GenericRestrict1[10];
    2. genericRestrict1s[0]=genericRestrictString;
    3. }

    }

泛型的主要使用包括 泛型类,泛型接口,泛型方法 三种形式

3. 泛型类

  1. //此处T可以随便写为任意标识,常见的如T、E、K、V等形式的参数常用于表示泛型
  2. //在实例化泛型类时,必须指定T的具体类型
  3. public class Generic<T>{
  4. //key这个成员变量的类型为T,T的类型由外部指定
  5. private T key;
  6. public Generic(T key) { //泛型构造方法形参key的类型也为T,T的类型由外部指定
  7. this.key = key;
  8. }
  9. public T getKey(){ //泛型方法getKey的返回值类型为T,T的类型由外部指定
  10. return key;
  11. }
  12. }

注意:

  1. 泛型的类型参数只能是类类型,不能是简单类型(8种基本类型)
  2. 在使用泛型类的时候,不一定要传入泛型类型实参, 如果不传入泛型类型实参的话,在泛型类中使用泛型的方法或成员变量定义的类型可以为任何的类型

4. 泛型接口

泛型接口与泛型类的定义及使用基本相同。泛型接口常被用在各种类的生产器中,可以看一个例子:

  1. //定义一个泛型接口
  2. public interface Generator<T> {
  3. public T next();
  4. }

当实现泛型接口的类,未传入泛型实参时:

  1. /** * 未传入泛型实参时,与泛型类的定义相同,在声明类的时候,需将泛型的声明也一起加到类中 * 即:class FruitGenerator<T> implements Generator<T>{ * 如果不声明泛型,如:class FruitGenerator implements Generator<T>,编译器会报错:"Unknown class" */
  2. class FruitGenerator<T> implements Generator<T>{
  3. @Override
  4. public T next() {
  5. return null;
  6. }
  7. }

当实现泛型接口的类,传入泛型实参时:

  1. /** * 传入泛型实参时: * 定义一个生产器实现这个接口,虽然我们只创建了一个泛型接口Generator<T> * 但是我们可以为T传入无数个实参,形成无数种类型的Generator接口。 * 在实现类实现泛型接口时,如已将泛型类型传入实参类型,则所有使用泛型的地方都要替换成传入的实参类型 * 即:Generator<T>,public T next();中的的T都要替换成传入的String类型。 */
  2. public class FruitGenerator implements Generator<String> {
  3. private String[] fruits = new String[]{ "Apple", "Banana", "Pear"};
  4. @Override
  5. public String next() {
  6. Random rand = new Random();
  7. return fruits[rand.nextInt(3)];
  8. }
  9. }

5. 泛型方法

  1. /** * 泛型方法的基本介绍 * @param tClass 传入的泛型实参 * @return T 返回值为T类型 * 说明: * 1)public 与 返回值中间<T>非常重要,可以理解为声明此方法为泛型方法。 * 2)只有声明了<T>的方法才是泛型方法,泛型类中的使用了泛型的成员方法并不是泛型方法。 * 3)<T>表明该方法将使用泛型类型T,此时才可以在方法中使用泛型类型T。 * 4)与泛型类的定义一样,此处T可以随便写为任意标识,常见的如T、E、K、V等形式的参数常用于表示泛型。 */
  2. public <T> T genericMethod(Class<T> tClass)throws InstantiationException ,
  3. IllegalAccessException{
  4. T instance = tClass.newInstance();
  5. return instance;
  6. }
  7. 调用泛型方法
  8. Object obj = genericMethod(Class.forName("com.test.test"));

泛型方法举例

  1. public class GenericTest {
  2. //这个类是个泛型类,在上面已经介绍过
  3. public class Generic<T>{
  4. private T key;
  5. public Generic(T key) {
  6. this.key = key;
  7. }
  8. //我想说的其实是这个,虽然在方法中使用了泛型,但是这并不是一个泛型方法。
  9. //这只是类中一个普通的成员方法,只不过他的返回值是在声明泛型类已经声明过的泛型。
  10. //所以在这个方法中才可以继续使用 T 这个泛型。
  11. public T getKey(){
  12. return key;
  13. }
  14. /** * 这个方法显然是有问题的,在编译器会给我们提示这样的错误信息"cannot reslove symbol E" * 因为在类的声明中并未声明泛型E,所以在使用E做形参和返回值类型时,编译器会无法识别。 public E setKey(E key){ this.key = keu } */
  15. }
  16. /** * 这才是一个真正的泛型方法。 * 首先在public与返回值之间的<T>必不可少,这表明这是一个泛型方法,并且声明了一个泛型T * 这个T可以出现在这个泛型方法的任意位置. * 泛型的数量也可以为任意多个 * 如:public <T,K> K showKeyName(Generic<T> container){ * ... * } */
  17. public <T> T showKeyName(Generic<T> container){
  18. System.out.println("container key :" + container.getKey());
  19. //当然这个例子举的不太合适,只是为了说明泛型方法的特性。
  20. T test = container.getKey();
  21. return test;
  22. }
  23. //这也不是一个泛型方法,这就是一个普通的方法,只是使用了Generic<Number>这个泛型类做形参而已。
  24. public void showKeyValue1(Generic<Number> obj){
  25. Log.d("泛型测试","key value is " + obj.getKey());
  26. }
  27. //这也不是一个泛型方法,这也是一个普通的方法,只不过使用了泛型通配符?
  28. //同时这也印证了泛型通配符章节所描述的,?是一种类型实参,可以看做为Number等所有类的父类
  29. public void showKeyValue2(Generic<?> obj){
  30. Log.d("泛型测试","key value is " + obj.getKey());
  31. }
  32. /** * 这个方法是有问题的,编译器会为我们提示错误信息:"UnKnown class 'E' " * 虽然我们声明了<T>,也表明了这是一个可以处理泛型的类型的泛型方法。 * 但是只声明了泛型类型T,并未声明泛型类型E,因此编译器并不知道该如何处理E这个类型。 public <T> T showKeyName(Generic<E> container){ ... } */
  33. /** * 这个方法也是有问题的,编译器会为我们提示错误信息:"UnKnown class 'T' " * 对于编译器来说T这个类型并未项目中声明过,因此编译也不知道该如何编译这个类。 * 所以这也不是一个正确的泛型方法声明。 public void showkey(T genericObj){ } */
  34. public static void main(String[] args) {
  35. }
  36. }

6. 泛型通配符

泛型通配符的种类:

一. <? extends T> 指定了泛型类型的上界

  1. 用于方法形参,限定只能传T以及T的子类
  2. 用于父类引用 ,主要用于安全地访问数据,可以访问 T 及其子类型,并且只能写入null

二. <? super T> 指定了泛型类型的下界

  1. 用于方法形参,限定只能传T以及T的父类
  2. 用于父类引用 ,主要用于安全地写入数据,可以写入Child及其子类型

三. <?> 指定了没有限制的泛型类型

  1. 用于方法形参,无任何限制,可以传任何类型
  2. 用于父类引用 ,只能写入null

    /*

    Description: 泛型通配符测试类 /
    public class GenericByWildcard {

    1. private static void print(GenericClass<Fruit> fruitGenericClass) {
    2. System.out.println(fruitGenericClass.getData().getColor());
    3. }
    4. private static void use() {
    5. GenericClass<Fruit> fruitGenericClass = new GenericClass<>();
    6. print(fruitGenericClass);
    7. GenericClass<Orange> orangeGenericClass = new GenericClass<>();
    8. //类型不匹配,可以使用<? extends Parent> 来解决

    // print(orangeGenericClass);

    1. }
    2. /** * <? extends Parent> 指定了泛型类型的上界 * 用于方法形参 ,限定只能传Fruit已经fruit的子类 */
    3. private static void printExtends(GenericClass<? extends Fruit> genericClass) {
    4. System.out.println(genericClass.getData().getColor());
    5. }
    6. public static void useExtend() {
    7. GenericClass<Fruit> fruitGenericClass = new GenericClass<>();
    8. printExtends(fruitGenericClass);
    9. GenericClass<Orange> orangeGenericClass = new GenericClass<>();
    10. printExtends(orangeGenericClass);
    11. GenericClass<Food> foodGenericClass = new GenericClass<>();
    12. //Food是Fruit的父类,超过了泛型上界范围,类型不匹配

    // printExtends(foodGenericClass);

    1. //用于父类引用
    2. //表示GenericClass的类型参数的上界是Fruit
    3. GenericClass<? extends Fruit> extendFruitGenericClass = new GenericClass<>();
    4. Apple apple = new Apple();
    5. Fruit fruit = new Fruit();
    6. /* * 道理很简单,? extends X 表示类型的上界,类型参数是X的子类,那么可以肯定的说, * get方法返回的一定是个X(不管是X或者X的子类)编译器是可以确定知道的。 * 但是set方法只知道传入的是个X,至于具体是X的那个子类,不知道。 * 总结:主要用于安全地访问数据,可以访问X及其子类型,并且只能写入null。 */

    // extendFruitGenericClass.setData(apple);
    // extendFruitGenericClass.setData(fruit);

    1. fruit = extendFruitGenericClass.getData();
    2. }
    3. /** * <? super Child> 指定了泛型类型的下界 * 只能传apple 以及 apple的父类 */
    4. public static void printSuper(GenericClass<? super Apple> genericClass) {
    5. System.out.println(genericClass.getData());
    6. }
    7. public static void useSuper() {
    8. GenericClass<Food> foodGenericClass = new GenericClass<>();
    9. printSuper(foodGenericClass);
    10. GenericClass<Fruit> fruitGenericClass = new GenericClass<>();
    11. printSuper(fruitGenericClass);
    12. GenericClass<Apple> appleGenericClass = new GenericClass<>();
    13. printSuper(appleGenericClass);
    14. GenericClass<HongFuShiApple> hongFuShiAppleGenericClass = new GenericClass<>();
    15. // HongFuShiApple 是Apple的子类,达不到泛型下界,类型不匹配

    // printSuper(hongFuShiAppleGenericClass);

    1. GenericClass<Orange> orangeGenericClass = new GenericClass<>();
    2. // Orange和Apple是兄弟关系,没有继承关系,类型不匹配

    // printSuper(orangeGenericClass);

    1. //用于父类引用
    2. //表示GenericClass的类型参数的下界是Apple
    3. GenericClass<? super Apple> supperAppleGenericClass = new GenericClass<>();
    4. supperAppleGenericClass.setData(new Apple());
    5. supperAppleGenericClass.setData(new HongFuShiApple());
    6. /* * ? super X 表示类型的下界,类型参数是X的超类(包括X本身), * 那么可以肯定的说,get方法返回的一定是个X的超类,那么到底是哪个超类?不知道, * 但是可以肯定的说,Object一定是它的超类,所以get方法返回Object。 * 编译器是可以确定知道的。对于set方法来说,编译器不知道它需要的确切类型,但是X和X的子类可以安全的转型为X。 * 总结:主要用于安全地写入数据,可以写入X及其子类型。 */

    // supperAppleGenericClass.setData(new Fruit());

    1. //get方法只会返回一个Object类型的值。
    2. Object data = supperAppleGenericClass.getData();
    3. }
    4. /** * <?> 指定了没有限定的通配符 */
    5. public static void printNonLimit(GenericClass<?> genericClass) {
    6. System.out.println(genericClass.getData());
    7. }
    8. public static void useNonLimit() {
    9. GenericClass<Food> foodGenericClass = new GenericClass<>();
    10. printNonLimit(foodGenericClass);
    11. GenericClass<Fruit> fruitGenericClass = new GenericClass<>();
    12. printNonLimit(fruitGenericClass);
    13. GenericClass<Apple> appleGenericClass = new GenericClass<>();
    14. printNonLimit(appleGenericClass);
    15. GenericClass<?> genericClass = new GenericClass<>();
    16. //setData 方法不能被调用, 甚至不能用 Object 调用;

    // genericClass.setData(foodGenericClass);
    // genericClass.setData(new Object());

    1. //返回值只能赋给 Object
    2. Object object = genericClass.getData();
    3. }

    }

参考链接:
Java泛型详解
java 泛型详解-绝对是对泛型方法讲解最详细的,没有之一
Java总结篇系列:Java泛型
菜鸟教程
聊一聊-JAVA 泛型中的通配符 T,E,K,V,?

发表评论

表情:
评论列表 (有 0 条评论,81人围观)

还没有评论,来说两句吧...

相关阅读

    相关 文章java

    一、泛型的定义和意义 1. 定义 泛型,即“参数化类型”。就是将类型由原来的具体的类型参数化,类似于方法中的变量参数,此时类型也定义成参数形式(可以称之为类型形参)

    相关 文章Java枚举类型

    前言 Java枚举类型虽然在特定的场景下通途很大,比如:对系统代码、系统参数等使用枚举类型表示,不仅解决使用常量配置参数可读性差的问题,还可以对数据进行分类,本文详解Ja