Java 泛型

一,3种泛型方式:泛型类、泛型接口、泛型方法


泛型类

复制代码
//此处T可以随便写为任意标识,常见的如T、E、K、V等形式的参数常用于表示泛型
//在实例化泛型类时,必须指定T的具体类型
public class Generic<T>{
    private T key;

    public Generic(T key) {
        this.key = key;
    }

    public T getKey(){
        return key;
    }
}

泛型接口

复制代码
public interface Generator<T> {
    public T next();
}

// 实现泛型接口时,可以传入或者不传实际类型:
public class FruitGenerator<T> implements Generator<T>
public class FruitGenerator implements Generator<String>

泛型方法

复制代码
// 只有声明了<T>的方法才是泛型方法,泛型类中的使用了泛型的成员方法并不是泛型方法
// 此处T可以随便写为任意标识,常见的如T、E、K、V等形式的参数常用于表示泛型
public <T> T genericMethod(Class<T> tClass) {
        T instance = tClass.newInstance();
        return instance;
}
  • 静态方法要使用泛型的话,必须将静态方法也定义成泛型方法

    public class StaticGenerator<T> {
    public static <T> void show(T t){

    复制代码
      }

    }

  • 泛型方法中添加上下边界的时候,必须在权限声明与返回值之间的<T>上添加上下边界,即在泛型声明的时候添加。

    // 编译器会报错
    public <T> T showKeyName(Generic<T extends Number> container)

    public <T extends Number> T showKeyName(Generic<T> container){
    System.out.println("container key :" + container.getKey());
    T test = container.getKey();
    return test;
    }

二,Tips


1,类型擦除:泛型类型只在编译阶段有效。
2,不能创建一个确切类型的泛型数组,而使用通配符?是可以的。

  • ❌\] List\\[\] ls = new ArrayList\\[10

  • ✅\] List\\[\] ls = new ArrayList\\[10\];

3,无法对泛型代码直接使用 instanceof 关键字,而使用通配符?是可以的。

  • ❌\] list instanceof ArrayList\

4,注意两种继承情况
① 第一种

复制代码
public void showKeyValue(Generic<Number> obj){
    Log.d("泛型测试","key value is " + obj.getKey());
}

Generic<Integer> gInteger = new Generic<Integer>(123);
Generic<Number> gNumber = new Generic<Number>(456);

[✅] showKeyValue(gNumber);
[❌] showKeyValue(gInteger); //编译报错

② 第二种

复制代码
class Fruit {
    @Override
    public String toString() {
        return "fruit";
    }
}

class Apple extends Fruit {
    @Override
    public String toString() {
        return "apple";
    }
}

class GenerateTest<T> {
    public void show_1(T t) {
        System.out.println(t.toString());
    }
}

public static void main(String[] args) {
    Apple apple = new Apple();
    GenerateTest<Fruit> generateTest = new GenerateTest<Fruit>();
    //apple是Fruit的子类,所以这里可以
    [✅]generateTest.show_1(apple);
}    
相关推荐
任风雨1 天前
13.2.3.Tomcat
java·tomcat
qq_336313931 天前
java基础-字符串
java
纵有疾風起1 天前
C++—string(1):string类的学习与使用
开发语言·c++·经验分享·学习·开源·1024程序员节
Molesidy1 天前
【随笔】【QT】QT5.15.2版本的最新下载方式!!!
开发语言·qt
二进制person1 天前
Java EE初阶 --多线程2
java·开发语言
yue0081 天前
C#理论学习-WinForm实践开发教程总结
开发语言·学习·c#
007php0071 天前
某游戏大厂 Java 面试题深度解析(四)
java·开发语言·python·面试·职场和发展·golang·php
Mr.Jessy1 天前
Web APIs学习第一天:获取 DOM 对象
开发语言·前端·javascript·学习·html
午安~婉1 天前
javaScript八股问题
开发语言·javascript·原型模式
想不明白的过度思考者1 天前
Rust——异步递归深度指南:从问题到解决方案
开发语言·后端·rust