概览
集合框架的源码经常见到“? extends E”、“? super T”。本篇文章以实例+注释讲讲“有限通配符的参数化类型”的创建、存值以及取值。
这两种都是限定类的取值范围的写法。“? 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 PriorityQueueto PriorityQueue //PriorityQueue pq = new PriorityQueue (); PriorityQueue pq = new PriorityQueue (); // 无法直接放入,因为无法保证存放类与Son的关系 //The method add(capture#1-of ? extends Parent) in the type PriorityQueue is not applicable for the arguments (Son) //pq.add(new Son())); //The method add(capture#1-of ? extends Parent) in the type PriorityQueue is not applicable for the arguments (Son) //pq.add(new Parent()); //null不受类型限定,但PriorityQueue不允许为空,会抛出空指针异常 //pq.add(null); // 间接存放 PriorityQueue pqs = new PriorityQueue (); pqs.add(new Son("1")); pqs.add(new Son("2")); pqs.add(new Son("3")); pqs.add(new Son("4")); PriorityQueue 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 PriorityQueueto PriorityQueue //PriorityQueue pq = new PriorityQueue (); PriorityQueue pq = new PriorityQueue (); // 可存放临界类的子类,因为任一“? 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 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{ 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>。
更多有意思的内容,欢迎访问笔者小站:
推荐阅读
。