码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 了解泛型的使用


    目录

    1.泛型类的定义

    1.1 语法

    1.2 简单实例

    1.3 加入静态内部类的示例

    1.4 加入继承或实现的示例

    2. 泛型类的使用

    2.1 语法

    2.2 示例

    2.3 类型推导(Type Inference)

    3. 泛型类的定义——类型边界

    4.1 语法

     4.2 示例

    5. 泛型类的使用——通配符

    5.1 基本使用

    5.2 通配符——上界

    5.3 通配符——下界

    6. 泛型中的父子类型

     7. 泛型方法

    7.1 语法


    1.泛型类的定义

    1.1 语法

    class 泛型类名称<类型形参列表> { 
    // 这里可以使用类型参数

    }

    1. class ClassName {
    2. }
    class 泛型类名称 < 类型形参列表 > extends 继承类 /* 这里可以使用类型参数 */ {
    // 这里可以使用类型参数
    }
    1. class ClassName extends ParentClass {
    2. // 可以只使用部分类型参数
    3. }

     规范:类型形参一般使用一个大写字母表示,常用的名称有:

    E 表示 Element

    K 表示 key

    V 表示 Value

    N 表示 Number

    T 表示 Type

    S,U,V 表示第二、第三、第四个类型

    1.2 简单实例

    1. //为泛指,自己定义类型
    2. //输出时自动对类型强制转换,不许自己转
    3. class ArrayList {
    4. private E[] elem;
    5. private int size;
    6. public ArrayList() {
    7. //this.elem = new E[10];//泛型不能实例化
    8. this.elem = (E[])new Object[10];
    9. }
    10. public void add(E val) {
    11. this.elem[size] = val;
    12. size++;
    13. }
    14. public E get(int pos) {
    15. return this.elem[pos];
    16. }
    17. }
    18. public class Test {
    19. public static void main(String[] args) {
    20. //通过泛型,可以点击new一个直接想要的类型对象
    21. ArrayList arrayList1 = new ArrayList<>();
    22. arrayList1.add("fly");
    23. String ret1 = arrayList1.get(0);
    24. System.out.println(ret1);
    25. System.out.println("-------------");
    26. ArrayList arrayList2 = new ArrayList<>();
    27. arrayList2.add(12);
    28. int ret2 = arrayList2.get(0);
    29. System.out.println(ret2);
    30. System.out.println("-------------");
    31. ArrayList arrayList3 = new ArrayList<>();
    32. arrayList3.add(true);
    33. boolean flg = arrayList3.get(0);
    34. System.out.println(flg);
    35. }
    36. }

    上诉代码定义了三个类型:string 、Integer 、boolean,然后调用泛型类即可。

    1.3 加入静态内部类的示例

    定义一个泛型类链表

    1. public class MyLinkedList {
    2. static class Node {
    3. private E value;
    4. private Node next;
    5. private Node(E e) {
    6. value = e;
    7. next = null;
    8. }
    9. }
    10. private Node head;
    11. private int size;
    12. public MyLinkedList() {
    13. head = null;
    14. size = 0;
    15. }
    16. //头插
    17. public void pushFront(E e) {
    18. Node node = new Node<>(e);
    19. node.next = head; head = node;
    20. size++;
    21. }
    22. //尾插
    23. public void pushBack(E e) {
    24. if (size == 0) {
    25. pushFront(e);
    26. return;
    27. }
    28. Node cur = head;
    29. while (cur.next != null) {
    30. cur = cur.next;
    31. }
    32. cur.next = new Node(e);
    33. size++;
    34. }
    35. }

    1.4 加入继承或实现的示例

    1. public interface MyList {
    2. // 尾插 void add(E e);
    3. // 尾删 E remove();
    4. }
    5. public class MyArrayList implements MyList {
    6. //
    7. }

    2. 泛型类的使用

    2.1 语法

    泛型类<类型实参> 变量名; //定义一个泛型类引用

    new 泛型类<类型实参> (构造方法实参);//实例化一个泛型类对象

    2.2 示例

    MyLinkedList list = new MyLinkedList();

    2.3 类型推导(Type Inference)

    当编译器可以根据上下文推导出类型实参时,可以省略类型实参的填写,直接强转为需要的类型
    MyLinkedList list = new MyLinkedList();

    3. 泛型类的定义——类型边界

    在定义泛型类时,有时需要对传入的类型变量做一定的约束,可以通过类型边界来约束。

    4.1 语法

    class 泛型类名称<类型形参 extends 类型边界> {

    }

     4.2 示例

    1)

    1. public class MyArrayListextends Number> {
    2. }
    只接受 Number 的子类型作为 E 的类型实参
    1. MyArrayList l1; // 正常,因为 Integer 是 Number 的子类型
    2. MyArrayList l2; // 编译错误,因为 String 不是 Number 的子类型

    2)求数组最大值

    1. class Algextends Comparable> {
    2. public T findMax(T[] array) {
    3. T max = array[0];
    4. for (int i = 0; i < array.length-1; i++) {
    5. if (max.compareTo(array[i]) < 0) {
    6. max = array[i];
    7. }
    8. }
    9. return max;
    10. }
    11. }
    12. public class TestDemo {
    13. public static void main(String[] args) {
    14. Alg alg = new Alg<>();
    15. Integer[] array = {2,4,1,23,2};
    16. System.out.println(alg.findMax(array));
    17. }
    18. }

    5. 泛型类的使用——通配符

    5.1 基本使用

    ?用于在泛型的使用,即为通配符

    示例

    1. public class MyArrayList {...}
    2. // 可以传入任意类型的
    3. MyArrayList public static void printAll(MyArrayList list) { ... }
    4. // 以下调用都是正确的
    5. printAll(new MyArrayList());
    6. printAll(new MyArrayList());
    7. printAll(new MyArrayList());
    8. printAll(new MyArrayList());
    9. printAll(new MyArrayList());

      5.2 通配符——上界

      extends 上界 >

      示例

      1. // 可以传入类型实参是 Number 子类的任意类型的 MyArrayList
      2. public static void printAll(MyArrayList list {
      3. //....
      4. }
      5. //以下调用是正确的
      6. printAll(new MyArrayList());
      7. printAll(new MyArrayList());
      8. printAll(new MyArrayList());
      9. //以下调用是错误的
      10. printAll(new MyArrayList());
      11. printAll(new MyArrayList());

        5.3 通配符——下界

        语法

        示例

        1. // 可以传入类型实参是 Integer 父类的任意类型的 MyArrayList
        2. public static void printAll(MyArrayListsuper Integer> list) { ... }
        3. // 以下调用都是正确的
        4. printAll(new MyArrayList());
        5. printAll(new MyArrayList());
        6. printAll(new MyArrayList());
        7. // 以下调用是编译错误的
        8. printAll(new MyArrayList());
        9. printAll(new MyArrayList());
        10. 6. 泛型中的父子类型

          public class MyArrayList { ... } 


          // MyArrayList 不是 MyArrayList 的父类型 
          // MyArrayList 也不是 MyArrayList 的父类型 

          // 需要使用通配符来确定父子类型 
          // MyArrayList 是 MyArrayList 的父类型
          // MyArrayList 是 MyArrayList 的父类型 

           7. 泛型方法

          7.1 语法

          方法限定符 < 类型形参列表 > 返回值类型 方法名称 ( 形参列表 ) { ... }

          示例 

          1. public class Util {
          2. public static void swap(E[] array, int i, int j) {
          3. E t = array[i];
          4. array[i] = array[j];
          5. array[j] = t;
          6. }
          7. }

        11. 相关阅读:
          遇到的题目
          英伟达开源 3400 亿参数模型;苹果 iOS 18 紧急 SOS 新增实时视频功能丨 RTE 开发者日报 Vol.225
          PyTorch for Audio + Music Processing(1) :Course Overview(课程大纲)
          MySQL练习题15道
          麻雀算法极限学习机SSA-ELM回归预测及其MATLAB代码实现
          P2433 【深基1-2】小学数学 N 合一题解
          ESP8266 WiFi物联网智能插座—电能计量
          PMP 报名有什么条件?容易满足吗?
          【毕业设计】中文汉字识别算法 - 深度学习 卷积神经网络 机器视觉 OCR
          SQL server 2008 r2 安装教程
        12. 原文地址:https://blog.csdn.net/m0_60494863/article/details/125676068
          • 最新文章
          • 沪漂五周年了:我越来越迷茫了
            Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
            MySQL-Seconds_behind_master的精度误差
            [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
            AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
            Agent OS :五种驯服不确定性的范式
            PortSwigger SQL注入LAB11
            数据库即时编译JIT
            [Begin]AI Learn Data Day 0
            深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
          • 热门文章
          • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
            奉劝各位学弟学妹们,该打造你的技术影响力了!
            五年了,我在 CSDN 的两个一百万。
            Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
            面试官都震惊,你这网络基础可以啊!
            你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
            心情不好的时候,用 Python 画棵樱花树送给自己吧
            通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
            13 万字 C 语言从入门到精通保姆级教程2021 年版
            10行代码集2000张美女图,Python爬虫120例,再上征途
          小工具 小游戏
          Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

          京公网安备 11010502049817号