JavaSE--泛型

发布时间 2023-08-15 22:24:08作者: 洛小依ovo

一、泛型

1、泛型语法机制

  泛型这种语法机制,只在程序编译阶段起作用,只是给编译器参考的。(运行阶段泛型没用!)

2、泛型的好处

  第一:集合中存储的元素类型统一了。

  第二:从集合中取出的元素类型是泛型指定的类型,不需要进行大量的“向下转型”

3、泛型的缺点

  导致集合中存储的元素缺乏多样性。大多数业务中,集合中元素的类型还是统一的。

public class GenericTest01 {
    public static void main(String[] args) {

        // 使用泛型List<Animal>之后,表示List集合中只允许存储Animal类型的数据。
        // 用泛型来指定集合中存储的数据类型
        List<Animal> myList = new ArrayList<Animal>();

        // 指定List集合中只能存储Animal,那么存储String就编译报错了
        //myList.add("abc");
        Cat c = new Cat();
        Bird b = new Bird();

        myList.add(c);
        myList.add(b);

        // 获取迭代器
        // 这个表示迭代器迭代的是Animal类型。
        Iterator<Animal> it = myList.iterator();
        while(it.hasNext()){
            // 使用泛型之后,每一次迭代返回的数据都是Animal类型。
            //Animal a = it.next();
            // 这里不需要进行强制类型转换了。直接调用。
            //a.move();

            // 调用子类型特有的方法还是需要向下转换的
            Animal a = it.next();
            if(a instanceof Cat) {
                Cat x = (Cat)a;
                x.catchMouse();
            }
            if(a instanceof Bird) {
                Bird y = (Bird)a;
                y.fly();
            }
        }
    }
}

class Animal {
    public void move(){
        System.out.println("动物在移动!");
    }
}
class Cat extends Animal {
    // 特有方法
    public void catchMouse(){
        System.out.println("猫抓老鼠!");
    }
}
class Bird extends Animal {
    // 特有方法
    public void fly(){
        System.out.println("鸟儿在飞翔!");
    }
}

4、自动类型推断(又称为钻石表达式)

public class GenericTest02 {
    public static void main(String[] args) {
        // ArrayList<这里的类型会自动推断>(),前提是JDK8之后才允许
        // 自动类型推断,钻石表达式  就是后面这块<>这个长得像钻石,叫做钻石表达式
        List<Animal> myList = new ArrayList<>();

        myList.add(new Animal());
        myList.add(new Cat());
        myList.add(new Bird());

        // 遍历
        Iterator<Animal> it = myList.iterator();
        while(it.hasNext()){
            Animal a = it.next();
            a.move();
        }
        // new ArrayList<>();这个<>叫钻石表达式
        List<String> strList = new ArrayList<>();
        strList.add("http://www.126.com");
        strList.add("http://www.baidu.com");

        // 遍历
        Iterator<String> it2 = strList.iterator();
        while(it2.hasNext()){
            String s = it2.next();
            // 直接调用String类的substring方法截取字符串
            String newString = s.substring(7);
            System.out.println(newString);
        }
    }
}

5、自定义泛型

// <>尖括号中是一个标识符,随便写
// 一般写E和T
// E是element,T是Type
public class GenericTest03<T> {
    public void doSome(T o){
        System.out.println(o);
    }

    public static void main(String[] args) {

        // new对象的时候指定了泛型是:String类型
        GenericTest03<String> gt = new GenericTest03<>();
        // 类型不匹配
        //gt.doSome(100);
        gt.doSome("abc");

        
        
        GenericTest03<Integer> gt2 = new GenericTest03<>();
        gt2.doSome(100);

        MyIterator<Animal> mi2 = new MyIterator<>();
        Animal a = mi2.get();

        // 不用泛型就是Object类型
        /*GenericTest03 gt3 = new GenericTest03();
        gt3.doSome(new Object());*/
    }
}