? extends T与? super T

概览

集合框架的源码经常见到“? extends E”、“? super T”。本篇文章以实例+注释讲讲“有限通配符的参数化类型”的创建、存值以及取值。
image
这两种都是限定类的取值范围的写法。“? extends T”表示类的允许范围是T及其子类;“? super T”表示类的允许范围是T及其父类。也就是new的时候受到此约束。

存值:只要能保证存放类是指定类及其子类即可。null不受“? extends/super T”约束。

取值:“? extends T”取得的默认类型为上界T,“? super T”的默认类型为所有类的父类Object。

Demo

package generic;

import java.util.PriorityQueue;

public class Extend {
    public static void main(String[] args) {
        // ? extends T,T为临界类
        // extends限定了类的上界
        // Type mismatch: cannot convert from PriorityQueue<Person> to PriorityQueue<? extends Parent>
        //PriorityQueue<? extends Parent> pq = new PriorityQueue<Person>();
        PriorityQueue<? extends Parent> pq = new PriorityQueue<Son>();
        
        // 无法直接放入,因为无法保证存放类与Son的关系
        //The method add(capture#1-of ? extends Parent) in the type PriorityQueue<capture#1-of ? extends Parent> is not applicable for the arguments (Son)
        //pq.add(new Son()));
        //The method add(capture#1-of ? extends Parent) in the type PriorityQueue<capture#1-of ? extends Parent> is not applicable for the arguments (Son)
        //pq.add(new Parent());
        //null不受类型限定,但PriorityQueue不允许为空,会抛出空指针异常
        //pq.add(null);
        
        // 间接存放
        PriorityQueue<Son> pqs = new PriorityQueue<Son>();
        pqs.add(new Son("1"));
        pqs.add(new Son("2"));
        pqs.add(new Son("3"));
        pqs.add(new Son("4"));
        
        PriorityQueue<? extends Parent> pq1 = pqs;
        
        //取值
        Son s = (Son) pq1.poll();
        Parent p = pq1.poll();
        //Daughter d = (Daughter) pq1.poll(); // 编译通过,执行报错。类型转换异常。
        Person pp= pq1.poll();
        
        System.out.println(s.getName());
        System.out.println(p.getName());
        System.out.println(pp.getName());
    }
}
package generic;

import java.util.PriorityQueue;

public class Super {
    public static void main(String[] args) {
        // ? super T,T为临界类
        // super限制了下界
        // Type mismatch: cannot convert from PriorityQueue<Son> to PriorityQueue<? super Parent>
        //PriorityQueue<? super Parent> pq = new PriorityQueue<Son>();
        PriorityQueue<? super Parent> pq = new PriorityQueue<Person>();
        
        // 可存放临界类的子类,因为任一“? super T”也是其父类
        pq.add(new Son("1"));
        pq.add(new Daughter("2"));
        pq.add(new Parent("3"));
        // The method add(capture#4-of ? super Parent) in the type PriorityQueue<capture#4-of ? super Parent> is not applicable for the arguments (Person)
        //pq.add(new Person("4"));

        // 取值(默认Object,类型顺序必须与存放对应或者是其父类,否则类型转换错误)
        /*Parent p = (Parent) pq.poll();
        Daughter d = (Daughter) pq.poll();
        Son s = (Son) pq.poll();*/
        
        Parent p = (Parent) pq.poll();
        Parent d = (Parent) pq.poll();
        Parent s = (Parent) pq.poll();
        
        System.out.println(d.getName());
        System.out.println(p.getName());
        System.out.println(s.getName());
    }
}
package generic;

public class Person implements Comparable<Person>{
    protected String name;

    public Person(String name) {
        super();
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public int compareTo(Person o) {
        return o.name.compareTo(this.name);
    }
}
package generic;

public class Parent extends Person{
    private String name;

    public Parent(String name) {
        super(name);
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

public class Son extends Parent{
    private String name;

    public Son(String name) {
        super(name);
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

public class Daughter extends Parent{
    private String name;

    public Daughter(String name) {
        super(name);
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

说点什么

针对以上特性,java中有“PECS(“Producer Extends,Consumer Super”)”的说法。即如果要用参数化类型表示生产者,就使用<? extends T>;如果表示消费者,就使用<? super T>。

更多有意思的内容,欢迎访问笔者小站: rebey.cn

推荐阅读

Java 泛型: 什么是PECS(Producer Extends, Consumer Super)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值