集合框架的源码常常见到“? extends E”、“? super T”。本篇文章以实例+注释讲讲“有限通配符的参数化类型”的建立、存值以及取值。
这两种都是限定类的取值范围的写法。“? extends T”表示类的容许范围是T及其子类;“? super T”表示类的容许范围是T及其父类。也就是new的时候受到此约束。html
存值:只要能保证存放类是指定类及其子类便可。null不受“? extends/super T”约束。java
取值:“? extends T”取得的默认类型为上界T,“? super T”的默认类型为全部类的父类Object。app
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.cnide