博客信息

泛型限定

发布时间:『 2019-05-13 20:40』  博客类别:java基础  阅读(650)

通配符,也可以理解为占位符

泛型的限定:

Extends E:可以接受E类型或者E的子类型,上限定

Super E:可以接受E类型或者E的父类型

小李飞刀_集合框架


小李飞刀_集合框架


小李飞刀_集合框架



?与泛型T在使用上的区别


小李飞刀_集合框架


相关代码

package com.javaxl;

import java.util.*;

/**
 * @author 小李飞刀
 * @site www.javaxl.com
 * @company
 * @create  2019-05-14 7:04
 */
public class GenericDemo2 {
    public static void main(String[] args) {
//        method_1(args);
//        method_2(args);
        method_3(args);

    }

    /**
     * ?占位符的出现
     * @param args
     */
    public static void method_1(String[] args) {
        ArrayList<String> al = new ArrayList<>();
        al.add("java01");
        al.add("java02");
        al.add("java03");
        al.add("java04");

        ArrayList<Integer> al2 = new ArrayList<>();
        al2.add(11);
        al2.add(22);
        al2.add(33);
        al2.add(44);

//        print_Coll(al);
//        print_Coll(al2);

//        print_Coll2(al);
//        print_Coll2(al2);

        print_Coll3(al);
        print_Coll3(al2);

    }

    /**
     * ?上限定应用
     * @param args
     */
    public static void method_2(String[] args) {
        ArrayList<GenericDemo2_Person> al = new ArrayList<>();
        al.add(new GenericDemo2_Person("java01"));
        al.add(new GenericDemo2_Person("java02"));
        al.add(new GenericDemo2_Person("java03"));

        ArrayList<GenericDemo2_Student> al2 = new ArrayList<>();
        al2.add(new GenericDemo2_Student("java01"));
        al2.add(new GenericDemo2_Student("java02"));
        al2.add(new GenericDemo2_Student("java03"));

        print_Coll4(al);
        print_Coll4(al2);


    }

    /**
     * ?下限定应用
     *  当treeSet和treeSet2都传入GenericDemo2_Student_Comp时,是不可行的,因为comparator接口做了向上限定
     *  但是都传入GenericDemo2_Person_Comp是可行的,简单说实现Comparator接口,接口中泛型可以填写student的父类
     *  这样的话即可以比较student,也可以比较person
     *
     * @param args
     */
    public static void method_3(String[] args) {
        TreeSet<GenericDemo2_Student> treeSet = new TreeSet<>(new GenericDemo2_Person_Comp());
        treeSet.add(new GenericDemo2_Student("java01"));
        treeSet.add(new GenericDemo2_Student("java03"));
        treeSet.add(new GenericDemo2_Student("java02"));

        TreeSet<GenericDemo2_Person> treeSet2 = new TreeSet<>(new GenericDemo2_Person_Comp());
        treeSet2.add(new GenericDemo2_Person("java01"));
        treeSet2.add(new GenericDemo2_Person("java03"));
        treeSet2.add(new GenericDemo2_Person("java02"));
        print_Coll5(treeSet);
        print_Coll5(treeSet2);

    }

    /**
     * 不使用泛型    error
     * ArrayLt<String> al = new ArrayList<Integer>();
     * @param al
     */
    public static void print_Coll(ArrayList<String> al) {
        Iterator<String> it = al.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }

    /**
     * 使用泛型T
     *  可以使用泛型实例
     * @param al
     */
    public static <T> void print_Coll2(ArrayList<T> al) {
        Iterator<T> it = al.iterator();
        while(it.hasNext()){
            T t = it.next();
            System.out.println(t);
        }
    }

    /**
     * 使用?占位符,效果与泛型是一样的,但是不能获取具体引用数据类型的实例
     * @param al
     */
    public static void print_Coll3(ArrayList<?> al) {
        Iterator<?> it = al.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }

    /**
     * ?上限定
     * 若参数是ArrayList<GenericDemo2_Person> al
     *  那么当传入的是GenericDemo2_Person子类时?
     *  ArrayList<GenericDemo2_Person> al = new ArrayList<GenericDemo2_Student>();  这是一个error,编译期就不会通过
     *
     *  那么我们又想操作GenericDemo2_Person子类的集合怎么办?
     *  ?上限定就出现了
     *  改为? extends GenericDemo2_Person
     * @param al
     */
    public static void print_Coll4(ArrayList<? extends GenericDemo2_Person> al) {
        Iterator<?> it = al.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }

    /**
     * 配合下限定案例
     * @param al
     */
    public static void print_Coll5(Set<? extends GenericDemo2_Person> al) {
        Iterator<?> it = al.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
}

class GenericDemo2_Person{
    private String name;

    public GenericDemo2_Person() {
    }

    public GenericDemo2_Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class GenericDemo2_Student extends GenericDemo2_Person{
    public GenericDemo2_Student() {
    }

    public GenericDemo2_Student(String name) {
        super(name);
    }
}

class GenericDemo2_Worker extends GenericDemo2_Person{
    public GenericDemo2_Worker() {
    }

    public GenericDemo2_Worker(String name) {
        super(name);
    }
}

class GenericDemo2_Student_Comp implements Comparator<GenericDemo2_Student>{

    @Override
    public int compare(GenericDemo2_Student o1, GenericDemo2_Student o2) {
        return o1.getName().compareTo(o2.getName());
    }
}

/**
 * 如果比较的student时,那么这么定义Comparator<Student>
 * 如果比较的worker时,那么这么定义Comparator<Worker>
 *
 *     这样的话,那么光person下的子类都会衍生很多的比较器
 *     比较student、worker,由于Comparator<? super E>这种定义
 *     所以就有了下面这种写法,person下的所有子类都可以使用该比较器
 */
class GenericDemo2_Person_Comp implements Comparator<GenericDemo2_Person>{

    @Override
    public int compare(GenericDemo2_Person o1, GenericDemo2_Person o2) {
        return o1.getName().compareTo(o2.getName());
    }
}



关键字:     Java基础       集合框架       泛型  

备案号:湘ICP备19000029号

Copyright © 2018-2019 javaxl晓码阁 版权所有