泛型中? super T和? extends T的区别

£神魔★判官ぃ 2022-01-22 07:05 486阅读 0赞

经常发现有List<? super T>、Set<? extends T>的声明,是什么意思呢?<? super T>表示包括T在内的任何T的父类,<? extends T>表示包括T在内的任何T的子类

PECS

请记住PECS原则:生产者(Producer)使用extends,消费者(Consumer)使用super。

  • 生产者使用extends

如果你需要一个列表提供T类型的元素(即你想从列表中读取T类型的元素),你需要把这个列表声明成<? extends T>,比如List<? extends Integer>,因此你不能往该列表中添加任何元素。

  • 消费者使用super

如果需要一个列表使用T类型的元素(即你想把T类型的元素加入到列表中),你需要把这个列表声明成<? super T>,比如List<? super Integer>,因此你不能保证从中读取到的元素的类型。

  • 即是生产者,也是消费者

如果一个列表即要生产,又要消费,你不能使用泛型通配符声明列表,比如List

请参考java.util.Collections里的copy方法:(dest的元素类型 和src中的元素类型相同 或者是其父类)

  1. /**
  2. * Copies all of the elements from one list into another. After the
  3. * operation, the index of each copied element in the destination list
  4. * will be identical to its index in the source list. The destination
  5. * list must be at least as long as the source list. If it is longer, the
  6. * remaining elements in the destination list are unaffected. <p>
  7. *
  8. * This method runs in linear time.
  9. *
  10. * @param <T> the class of the objects in the lists
  11. * @param dest The destination list.
  12. * @param src The source list.
  13. * @throws IndexOutOfBoundsException if the destination list is too small
  14. * to contain the entire source List.
  15. * @throws UnsupportedOperationException if the destination list's
  16. * list-iterator does not support the <tt>set</tt> operation.
  17. */
  18. public static <T> void copy(List<? super T> dest, List<? extends T> src) {
  19. int srcSize = src.size();
  20. if (srcSize > dest.size())
  21. throw new IndexOutOfBoundsException("Source does not fit in dest");
  22. if (srcSize < COPY_THRESHOLD ||
  23. (src instanceof RandomAccess && dest instanceof RandomAccess)) {
  24. for (int i=0; i<srcSize; i++)
  25. dest.set(i, src.get(i));
  26. } else {
  27. ListIterator<? super T> di=dest.listIterator();
  28. ListIterator<? extends T> si=src.listIterator();
  29. for (int i=0; i<srcSize; i++) {
  30. di.next();
  31. di.set(si.next());
  32. }
  33. }
  34. }

泛型中使用通配符有两种形式:子类型限定<? extends xxx>和超类型限定<? super xxx>。

(1)子类型限定

下面的代码定义了一个Pair类,以及Employee,Manager和President类。

  1. public class Pair<T> {
  2. private T first;
  3. private T second;
  4. public Pair(T first, T second) {
  5. this.first = first;
  6. this.second = second;
  7. }
  8. public T getFirst() {
  9. return first;
  10. }
  11. public T getSecond() {
  12. return second;
  13. }
  14. public void setFirst(T newValue) {
  15. first = newValue;
  16. }
  17. public void setSecond(T newValue) {
  18. second = newValue;
  19. }
  20. }
  21. class Employee {
  22. private String name;
  23. private double salary;
  24. public Employee(String n, double s) {
  25. name = n;
  26. salary = s;
  27. }
  28. public String getName() {
  29. return name;
  30. }
  31. public double getSalary() {
  32. return salary;
  33. }
  34. }
  35. class Manager extends Employee {
  36. public Manager(String n, double s) {
  37. super(n, s);
  38. }
  39. }
  40. <pre name="code" class="java">
  41. class President extends Manager {
  42. public President(String n, double s) {
  43. super(n, s);
  44. }
  45. }

现在要定义一个函数可以打印Pair

  1. public static void printEmployeeBoddies(Pair<Employee> pair) {
  2. System.out.println(pair.getFirst().getName() + ":" + pair.getSecond().getName());
  3. }

可是有一个问题是这个函数输入参数只能传递类型Pair,而不能传递Pair和Pair。例如下面的代码会产生编译错误


  1. Manager mgr1 = new Manager("Jack", 10000.99);
  2. Manager mgr2 = new Manager("Tom", 10001.01);
  3. Pair<Manager> managerPair = new Pair<Manager>(mgr1, mgr2);
  4. PairAlg.printEmployeeBoddies(managerPair);

之所以会产生编译错误,是因为Pair和Pair实际上是两种类型。

Center

由上图可以看出,类型Pair是类型Pair<? extends Employee>的子类型,所以为了解决这个问题可以把函数定义改成
public static void printEmployeeBoddies(Pair<? extends Employee> pair)

但是使用通配符会不会导致通过Pair<? extends Employee>的引用破坏Pair对象呢?例如:
Pair<? extends Employee> employeePair = managePair;employeePair.setFirst(new Employee(“Tony”, 100));
不用担心,编译器会产生一个编译错误。Pair<? extends Employee>参数替换后,我们得到如下代码
? extends Employee getFirst()
void setFirst(? extends Employee)
对于get方法,没问题,因为编译器知道可以把返回对象转换为一个Employee类型。但是对于set方法,编译器无法知道具体的类型,所以会拒绝这个调用。

(2)超类型限定

超类型限定和子类型限定相反,可以给方法提供参数,但是不能使用返回值。? super Manager这个类型限定为Manager的所有超类。

Center 1

Pair<? super Manager>参数替换后,得到如下方法

  1. ? super Manager getFirst()
  2. void setFirst(? super Manager)

编译器可以用Manager的超类型,例如Employee,Object来调用setFirst方法,但是无法调用getFirst,因为不能把Manager的超类引用转换为Manager引用。

超类型限定的存在是为了解决下面一类的问题。例如要写一个函数从Manager[]中找出工资最高和最低的两个,放在一个Pair中返回。


  1. public static void minMaxSal(Manager[] mgrs, Pair<? super Manager> pair) {
  2. if (mgrs == null || mgrs.length == 0) {
  3. return;
  4. }
  5. pair.setFirst(mgrs[0]);
  6. pair.setSecond(mgrs[0]);
  7. //TODO
  8. }

如此就可以这样调用

  1. Pair<? super Manager> pair = new Pair<Employee>(null, null);
  2. minMaxSal(new Manager[] {mgr1, mgr2}, pair);

发表评论

表情:
评论列表 (有 0 条评论,486人围观)

还没有评论,来说两句吧...

相关阅读