Skip to content

Java中,泛型用于编译阶段限制集合中元素的类型,或者限制类中某个属性的类型,编译过程中发生类型擦除,最终还是Object类型。

1. 集合中的泛型

集合默认可以存储任何类型的元素,即Object类型,当使用一个集合存储String类型元素时

不使用泛型
java
        List list = new ArrayList();
        list.add("a");
        // 当使用集合中的元素时,需要手动进行类型转换
        String str0 = (String) list.get(0);
        // 而且无法保证集合的正确性
        list.add(123);
        // 运行时报错类型转换异常 ClassCastException
        String str1 = (String) list.get(1);
使用泛型
java
        List<String> list = new ArrayList<>();
        list.add("a");
        // 当使用集合中的元素时,通过泛型可以确定,集合中都是指定类型,故不需要手动进行类型转换
        String str0 = list.get(0);
        // list.add(123); // 编译不通过

2. 类中泛型

类中使用泛型与集合中同理,使用泛型可以避免使用时的手动强制转换,并保证数据的合法性。

不使用泛型
java
    public static void main(String[] args) {
        Box box = new Box("abc");
        String value = (String) box.getValue();
        box.setValue(123);
        // 运行时报错类型转换异常 ClassCastException
        value = (String) box.getValue(); 
    }
    static class Box {
        private Object value;
        public Box(Object value) {
            this.value = value;
        }
        public Object getValue() {
            return value;
        }
        public void setValue(Object value) {
            this.value = value;
        }
    }
3. 使用泛型
java
    public static void main(String[] args) {
        Box<String> box = new Box<>("abc");
        String value = box.getValue();
        // box.setValue(123); 编译不通过
    }
    static class Box<T> {
        private T value;
        public Box(T value) {
            this.value = value;
        }
        public T getValue() {
            return value;
        }
        public void setValue(T value) {
            this.value = value;
        }
    }

3. ?和 extends 和 super

? 表示不确定的类型,通常配置 extends 或 super,限制泛型的范围

java
    public static void main(String[] args) {
        List<? super Cat> list = new ArrayList<>();
        list.add(new Cat());
        // list.add(new Dog()); // 不是 Cat 或其子类型
    }
    static class Cat {
        public void eat() {
            System.out.println("猫吃东西");
        }
    }
    static class Dog {
        public void eat() {
            System.out.println("狗吃东西");
        }
    }