码农知识堂 - 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. 相关阅读:
          【Tailwind CSS】当页面内容过少,怎样让footer保持在屏幕底部?
          QImageWriter
          数据库实验报告(六)
          SpringMVC对JSON的支持& SpringMVC的全局异常处理
          恒生Ptrade——盘口扫单功能介绍
          MYSQL第一章节DDL数据定义语言的操作(DDL-数据库操作,DDL-操作表-查询,DDL-操作表-修改,数据库的基本类型)
          在线教育项目【老师服务】
          leetcode 57. 插入区间
          8.2 JWT(代替Session)
          python:最小二乘法拟合原理及代码实现
        12. 原文地址:https://blog.csdn.net/m0_60494863/article/details/125676068
          • 最新文章
          • 攻防演习之三天拿下官网站群
            数据安全治理学习——前期安全规划和安全管理体系建设
            企业安全 | 企业内一次钓鱼演练准备过程
            内网渗透测试 | Kerberos协议及其部分攻击手法
            0day的产生 | 不懂代码的"代码审计"
            安装scrcpy-client模块av模块异常,环境问题解决方案
            leetcode hot100【LeetCode 279. 完全平方数】java实现
            OpenWrt下安装Mosquitto
            AnatoMask论文汇总
            【AI日记】24.11.01 LangChain、openai api和github copilot
          • 热门文章
          • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
            奉劝各位学弟学妹们,该打造你的技术影响力了!
            五年了,我在 CSDN 的两个一百万。
            Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
            面试官都震惊,你这网络基础可以啊!
            你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
            心情不好的时候,用 Python 画棵樱花树送给自己吧
            通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
            13 万字 C 语言从入门到精通保姆级教程2021 年版
            10行代码集2000张美女图,Python爬虫120例,再上征途
          Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
          正则表达式工具 cron表达式工具 密码生成工具

          京公网安备 11010502049817号