• Java 第二阶段提升编程能力【泛型】


    代码链接:https://download.csdn.net/download/qq_52354698/86401487?spm=1001.2014.3001.5501

    1. 泛型介绍

    1. 泛型又称参数化类型,是JDK5.0出现的新特性,解决数据类型的安全性问题。
    2. 在类声明或实例化时只要指定好需要的具体类型即可。
    3. Java 泛型可以保证程序在编译时没有发出警告,运行时就不会产生ClassCastException 异常。
    4. 泛型的作用是:可以在类声明时通过一个表示来表示类中某个属性的类型,或者是某个方法的返回值类型,或者是参数类型。
    class Person<E> {
    
        E s;
    
        public Person(E s){
            this.s = s;
        }
    
        public E f() {
            return s;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2. 泛型的语法

    1. 泛型的声明

    interface 接口 <T> {}class<K,V> p {}
    
    • 1

    说明:

    1. 其中,T,K,V 不代表值,而是表示类型。
    2. 任意字母都可以。

    2. 泛型的实例化

    要在类名后面指定类型参数的值(类型)。

    List<String> strList = new ArrayList<String>();
    Interor<Customer> iterator = customers.iterator();
    
    • 1
    • 2
    package com.qdu.generic;
    
    import java.util.*;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class GenericExercise {
    
        public static void main(String[] args) {
    
            HashSet<Student> hashSet = new HashSet<Student>();
            hashSet.add(new Student("jack", 18));
            hashSet.add(new Student("king", 28));
    
            for (Student o : hashSet) {
                System.out.println(o.getName() + " " + o.getAge());
            }
    
            HashMap<String, Student> hashMap = new HashMap<String, Student>();
            hashMap.put("tom", new Student("tom", 18));
            hashMap.put("sam", new Student("sam", 38));
    
            Set<Map.Entry<String, Student>> entries = hashMap.entrySet();
            Iterator<Map.Entry<String, Student>> iterator =  entries.iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Student> next = iterator.next();
                System.out.println(next.getKey() + " " + next.getValue());
            }
    
        }
    
    }
    
    class Student {
    
        private String name;
        private int age;
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    3. 注意事项和细节

    1. interface List{},public class HashSet{}
      说明:T、E只能是引用类型

    2. 在给泛型指定具体类型后,可以传入该类型或者其子类类型

    3. 自定义泛型

    1. 自定义泛型类

    class 类名<T, R...>{
    	成员
    }
    
    • 1
    • 2
    • 3
    1. 普通成员可以使用泛型(属性、方法)
    2. 使用泛型的数组,不能初始化
    3. 静态方法中不能使用类的泛型
    4. 泛型类的类型,是在创建对象时确定的(因为创建对象时,需要指定确定类型)
    5. 如果在创建对象时,没有指定类型,默认为Object

    2. 自定义泛型接口

    interface 接口名<T, R...>{
    }
    
    • 1
    • 2
    1. 接口中,静态成员也不能使用泛型(这个和泛型类规定一样)
    2. 泛型接口的类型,在继承接口或者实现接口时确定
    3. 没有指定类型,默认为Object

    3. 自定义泛型方法

    修饰符 <T, R...> 返回类型 方法名(参数列表){
    }
    
    • 1
    • 2
    1. 泛型方法,可以定义在普通类中,也可以定义在泛型类中
    2. 当放行方法被调用时,类型会确定
    3. public void eat(E e){},修饰符后没有 eat 方法不是泛型方法,而是使用了泛型

    4. 泛型的继承和通配符

    1. 泛型不具备继承性
      在这里插入图片描述

    2. :支持任意泛型类型都可以接受
    3. :支持A类以及A类的子类,规定了泛型的上限
    4. :支持A类以及A类的父类,不限于直接父类,规定了泛型的下限
  • 相关阅读:
    DOM跟BOM的区别
    【老生谈算法】matlab实现卡尔曼滤波算法源码——卡尔曼滤波
    ES6-03-模版字符串、对象的简化写法
    【HTML】常用标签总结
    Python的第三方日志库Loguru
    客快物流大数据项目(七十一):impala-shell命令参数
    leetcode236. 二叉树的最近公共祖先
    5年经验,没听过XFF漏洞
    idea springboot 自定义注释无效
    169. 多数元素
  • 原文地址:https://blog.csdn.net/qq_52354698/article/details/126358622