• java_泛型2


    一.泛型_泛型概述及好处 

       1).在定义集合时,我们是希望集合中只存储一种类型的引用,这时可以使用泛型:

          ArrayList<String> list = new ArrayList<>(); //JDK7以后可以这样写

          或者:

          ArrayList<String> list = new ArrayList<String>();//JDK7以前必须这样写

       2).泛型的好处:可以规定一个集合中只能存储什么一种固定的类型。当不小心存储其它类型时,编译器会编译错误。

          ArrayList<String> list = new ArrayList<>();

          list.add("abc");

          list.add(10);//编译错误

    二.泛型_定义和使用含有泛型的类

    package com.ahd.fanxing;

    public class Father<K,V> {
        K k;
        V v;
        public K getK(){
            return k;
        }

        public void setK(K k){
            this.k=k;
        }

        public V getV() {
            return v;
        }

        public void setV(V v) {
            this.v = v;
        }
    }

     

     

      注意:

       1).<E>:表示定义了一个泛型,它表示一种类型,只有在使用这个类时,才定义这个类型。

       2).<E>:语法:可以大写、可以小写、可以一个字母,也可以是多个字母。

     

    三.泛型_含有泛型的方法

    public class MyList {
        public <E> void show(E e1,E e2,E e3) {
            System.out.println("show()...");
        }
    }

    测试类:

    public class Demo {
        public static void main(String[] args) {
            MyList myList = new MyList();
    
            myList.<Integer>show(10,20,30);
    
            myList.<String>show("abc", "bbb", "ccc");
        }
    }

    四.泛型_含有泛型的接口

       1).定义方法同泛型类一样:

    public interface Animal<E> {
        public void show(E e);
        public E get();
    }

     

    泛型_泛型通配符

       1).? : 可以表示:具有任何泛型的集合对象

    public class Demo {
        public static void main(String[] args) {
            ArrayList<Object> objList = new ArrayList<>();
            ArrayList<Integer> intList = new ArrayList<>();
            ArrayList<String> strList = new ArrayList<>();
    
            /*fun1(objList);//OK的
            fun1(strList);//错误
            fun1(intList);//错误*/
            
            fun2(objList);
            fun2(strList);
            fun2(intList);
            
        }
        public static void fun1(ArrayList<Object> object) {
    
        }
        //需求:定义一个方法,可以接收具有任何泛型的ArrayList对象
        public static void fun2(ArrayList<?> object) {
    
        }
    }

    2).<? extends E>:表示可以接收:E及其E的子类泛型。设定了上限

         

    class Person{}
    class Student extends Person{}
    class JavaStudent extends Student{}
    class PHPStudent extends Student{}
    
    class Teacher extends Person{ }
    
    public class Demo {
        public static void main(String[] args) {
            ArrayList<Person> list1 = new ArrayList<>();
    
            ArrayList<Student> list2 = new ArrayList<>();
            ArrayList<JavaStudent> list3 = new ArrayList<>();
            ArrayList<PHPStudent> list4 = new ArrayList<>();
    
            ArrayList<Teacher> list5 = new ArrayList<>();
    
    //      fun(list1);//错误
            fun(list2);
            fun(list3);
            fun(list4);
    //      fun(list5);//错误
    
        }
    
        //需求:定义一个方法,可以接收具有Student及其子类泛型的ArrayList对象
        public static void fun(ArrayList<? extends Student> list) {
    
        }
    }

    3).<? super E>:表示可以接收:E及其E的父类泛型。设定了下限

    public class Demo {
        public static void main(String[] args) {
            ArrayList<Person> list1 = new ArrayList<>();
    
            ArrayList<Student> list2 = new ArrayList<>();
            ArrayList<JavaStudent> list3 = new ArrayList<>();
            ArrayList<PHPStudent> list4 = new ArrayList<>();
    
            ArrayList<Teacher> list5 = new ArrayList<>();
    
            fun(list1);
            fun(list2);
    //        fun(list3);//错误
    //        fun(list4);//错误
    //        fun(list5);//错误
    
        }
    
        //需求:定义一个方法,可以接收具有Student及”父类“类泛型的ArrayList对象
        public static void fun(ArrayList<? super Student> list) {
    
        }
    }

    -------------------------------------------------------------------------------

  • 相关阅读:
    arcsde 和oracle(双机热备)分布式安装(转载)
    ArcGIS Server分布式部署
    华为软件编程规范和范例(转载)
    常用Java开源库
    通过Word 2007发布Blog
    django中的models模块及数据库一些基本操作
    网页设计与后台程序解决方案模板引擎之Smarty
    Session 和 Cookie
    ul及li水平居中显示
    HTTP协议简介
  • 原文地址:https://www.cnblogs.com/aihuadung/p/10685872.html
Copyright © 2020-2023  润新知