当前位置: 首页 > news >正文

泛型、泛型上限、泛型下限、泛型通配符

DAY8.1 Java核心基础

泛型在这里插入图片描述

Generics 是指在类定义时不指定类中信息的具体数据类型,而是用一个标识符来代替,当外部实例化对象时再指定具体的数据类型。

在定义类或者接口时不明确指定类中信息的具体数据类型,在实例化时再来指定具体的数据类型

极大地提升了类的扩展性,一个类可以装载各种不同的数据类型

泛型可以指代类中的成员变量数据类型,方法的返回值数据类型以及方法的参数数据类型。

基本使用:

定义一个Demo不指定类中的具体数据类型,用标识符代替

public class Demo<T>{private T t;public Demo(T t) {this.t = t;}public T getT() {return t;}public void setT(T t) {this.t = t;}
}
public static void main(String[] args) {Demo<String> hello = new Demo<>("hello");System.out.println(hello.getT());Demo<Integer> integerDemo = new Demo<>(11);System.out.println(integerDemo.getT());
}

输出:
image-20250309103252419

泛型也可以同时写多个泛型

public class Demo<T,A,B>{private T t;private A a;private B b;public Demo(T t, A a, B b) {this.t = t;this.a = a;this.b = b;}@Overridepublic String toString() {return "Demo{" +"t=" + t +", a=" + a +", b=" + b +'}';}
}
public static void main(String[] args) {Demo<String, Integer, Float> hello = new Demo<>("hello", 1, 2.0f);System.out.println(hello);
}

image-20250309103908102

泛型通配符<?>:在不确定传入的数据类型的时候可以使用通配符

public static void main(String[] args) {ArrayList<Integer> integers = new ArrayList<>();test(integers);ArrayList<String> strings = new ArrayList<>();test(strings);
}
public static void  test(ArrayList<?> list) {System.out.println(list);
}

比如传入的参数是Interger类型和String类型,如果定义ArrayList list作为形参则String类型的数组无法传递

泛型的上限和下限

上限:类名<? extends A> 这个类型必须是A类的子类或者A类型本身
下限:类名<? super A> 这个类型必须是A类的父类或则A类型本身

public class Test {public static void main(String[] args) {ArrayList<Double> doubles = new ArrayList<>();doubles.add(1.0);test1(doubles);ArrayList<Object> strings = new ArrayList<>();strings.add("hello");test2(strings);}/*** 标识test1方法的list参数的类型是Number的子类或者是Number本身,比如 Integer、Double、Float...* @param list*/public static void test1(ArrayList<? extends Number> list) {System.out.println(list);}/*** 表示test2方法的list参数的类型是String的父类或者是String本身,String or Object* @param list*/public static void test2(ArrayList<? super String> list) {System.out.println(list);}
}

泛型接口

public interface MyInterface<T> {public T test();
}

实现类:

public class MyInterfaceImpl1 implements MyInterface<String>{public String t;public MyInterfaceImpl1(String t) {this.t = t;}@Overridepublic String test() {return t;}
}
public class MyInterfaceImpl2<T> implements MyInterface<T>{public T t;public MyInterfaceImpl2(T t) {this.t = t;}@Overridepublic T test() {return t;}
}

两个实现类,一个在实现的时候就定义了类型,一个没有定义,所以MyInterfaceImpl1就不能在使用的时候指定其它类型对象,就只能使用String类型

image-20250309110134125

正确测试代码:

public static void main(String[] args) {MyInterfaceImpl1 myInterfaceImpl1 = new MyInterfaceImpl1("123");System.out.println(myInterfaceImpl1.test());MyInterfaceImpl2<Integer> myInterfaceImpl2 = new MyInterfaceImpl2<>(123);System.out.println(myInterfaceImpl2.test());
}

http://www.mrgr.cn/news/93840.html

相关文章:

  • (更新完)LPZero: Language Model Zero-cost Proxy Search from Zero
  • 梯度计算中常用的矩阵微积分公式
  • How to install nacos 2.5 with podman
  • Java 大视界 -- Java 大数据在智能体育赛事运动员表现分析与训练优化中的应用(122)
  • ALG(Alloy+Loki+Grafana)轻量级日志系统
  • 华为eNSP:配置单区域OSPF
  • ​​《从事件冒泡到处理:前端事件系统的“隐形逻辑”》
  • Deepseek可以通过多种方式帮助CAD加速工作
  • Mybatis Generator 使用手册
  • DeepSeek私有化部署7:openEuler 24.03-LTS-SP1安装Open WebUI
  • MYSQL之创建数据库和表
  • 用Python写一个算24点的小程序
  • 【STM32】STM32系列产品以及新手入门的STM32F103
  • [总概]Vue2/3React Diff算法
  • 【经验分享】Ubuntu20.04编译RK3568 AI模型报错问题(已解决)
  • FPGA时序约束的几种方法
  • 【redis】五种数据类型和编码方式
  • 【2025前端高频面试题——系列二之vue生命周期:vue2】
  • 如何将本地已有的仓库上传到gitee (使用UGit)
  • 解锁DeepSpeek-R1大模型微调:从训练到部署,打造定制化AI会话系统