Java基础进阶泛型

导读:本篇文章讲解 Java基础进阶泛型,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1、JDK5.0之后推出的新特性:泛型

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

3、使用了泛型好处是什么?

  • 第一:集合中存储的元素类型统一了。
  • 第二:从集合中取出的元素类型是泛型指定的类型,不需要进行大量的“向下转型”!

4、泛型的缺点是什么?

  • 导致集合中存储的元素缺乏多样性!
  • 大多数业务中,集合中元素的类型还是统一的。所以这种泛型特性被大家所认可。

示例代码01:

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

        List myList = new ArrayList();

        // 准备对象
        Cat c = new Cat();
        Bird b = new Bird();

        // 将对象添加到集合当中
        myList.add(c);
        myList.add(b);

        // 遍历集合,取出每个Animal,让它move
        Iterator it = myList.iterator();
        while(it.hasNext()){
            // 没有这个语法,通过迭代器取出的就是Object
            //Animal a = it.next();
            Object obj = it.next();
            if(obj instanceof Animal){
                Animal a = (Animal) obj;
                a.move();
            }
        }

        //使用泛型
        // 使用JDK5之后的泛型机制
        // 使用泛型List<Animal>之后,表示List集合中只允许存储Animal类型的数据。
        // 用泛型来指定集合中存储的数据类型。
        List<Animal> l2 = new ArrayList<Animal>();
        // 指定List集合中只能存储Animal,那么存储String就编译报错了。
        // 这样用了泛型之后,集合中元素的数据类型更加统一了。
        Cat c1 = new Cat();
        Bird b1 = new Bird();

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

            // 调用子类型特有的方法还是需要向下转换的!
            Animal a1 = it1.next();
            if(a1 instanceof Bird){
                Bird b2 = (Bird) a1;
                b2.fly();
            }else if(a1 instanceof Cat){
                Cat c2 = (Cat) a1;
                c2.catchMouse();
            }

        }


    }
}

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("鸟儿在飞翔!");
    }
}

运行结果:

在这里插入图片描述

5、JDK之后引入了:自动类型推断机制。(又称为钻石表达式)

示例代码02:

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();
       }

       List<String> l1 = new ArrayList<>();
       l1.add("http://www.126.com");
       l1.add("http://www.baidu.com");
       l1.add("http://www.newstudy.com");
        Iterator<String> it1 = l1.iterator();
        while(it1.hasNext()){
            // 如果没有使用泛型
            /*
            Object obj = it2.next();
            if(obj instanceof String){
                String ss = (String)obj;
                ss.substring(7);
            }
             */
            // 直接通过迭代器获取了String类型的数据
            String s = it1.next();
            // 直接调用String类的substring方法截取字符串。
            String sub = s.substring(7);
            System.out.println(sub);
        }
    }
}

运行结果:

在这里插入图片描述

6、自定义泛型

自定义泛型可以吗?可以
自定义泛型的时候,<> 尖括号中的是一个标识符,随便写。
java源代码中经常出现的是:

  • E是Element单词首字母。
  • T是Type单词首字母。

示例代码03:

public class GenericTest03<标识符随便写> {

    public void doSome(标识符随便写 o){

    }

    public static void main(String[] args) {

        // new对象的时候指定了泛型是:String类型
        GenericTest03<String> s = new GenericTest03<>();
        s.doSome("123");
        //s.doSome(new Animal());//报错类型不匹配

        GenericTest03<Integer> i = new GenericTest03<>();
        i.doSome(123);
        //i.doSome("123");//报错类型不匹配

        MyGeneric<String> m = new MyGeneric<>();
        String s1 = m.getT();

        MyGeneric<Animal> m1 = new MyGeneric<>();
        Animal a = m1.getT();

        //不使用泛型返回的就是Object类型
        MyGeneric m3 = new MyGeneric();
        Object o = m3.getT();
    }


}

class MyGeneric<T>{

    public T getT(){
        return null;
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/87595.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!