Java泛型通配符的上下界和用法 ? extend T(上界) ? super T(下界)

 

引用链接:https://blog.csdn.net/weixin_39800144/article/details/81299046

 

1.二者的意思

? extends T :表示上界是T, ? 都是继承自T的,都是T的子类;
? super T :表示下界是T,?都是T的父类;

2.用法

例1,我们有多个对象,其中Cat,Dog,Chicken都是extends Animal的。

        List<? extends Animal> list = new ArrayList<Cat>();
        List<? extends Animal> list = new ArrayList<Dog>();
        List<? extends Animal> list = new ArrayList<Chicken>();

此时,List ? extends Animal list是无法得知这个list究竟可能是上面的哪一种,所以,list是无法 使用add方法的,但是我们可以读取到Anumal类型的数据。

例2,我们有DogFather1,DogFather2,DogFather3,都可能是Dog的父类。

        List<? super Dog> list = new ArrayList<DogFather1>();
        List<? super Dog> list = new ArrayList<DogFather2>();
        List<? super Dog> list = new ArrayList<DogFather3>();

这样,我们不能确定这个list里究竟是DogFather1,还是DogFather2,还是DogFather3,所以我们读取时无法得知读取的是什么类型,但是我们可以写入Dog及其子类对象,来保证正确性。

3.总结

第一、 频繁往外读取内容的,适合用 ? extends T;
第二、 经常往里插入的,适合用 ? super T;
(阿里JAVA开发手册强制这样使用,见集合处理的第6条)

这个用法的典型用法可以在java的集合类Collections源码中找到:

    public static <T> void copy(List<? super T> dest, List<? extends T> src) {
        int srcSize = src.size();
        if (srcSize > dest.size())
            throw new IndexOutOfBoundsException("Source does not fit in dest");

        if (srcSize < COPY_THRESHOLD ||
            (src instanceof RandomAccess && dest instanceof RandomAccess)) {
            for (int i=0; i<srcSize; i++)
                dest.set(i, src.get(i));
        } else {
            ListIterator<? super T> di=dest.listIterator();
            ListIterator<? extends T> si=src.listIterator();
            for (int i=0; i<srcSize; i++) {
                di.next();
                di.set(si.next());
            }
        }
    }

4. 最后的例子

public class Animal {}

public class Dog extends Animal {}

public class Collie exends Dog {}

public void test() {
   List<? extends Dog> extendsDogs = new ArrayList<>();
   List<? super Collie> superDogs = new ArraryList<>();
   
   Animal animal = new Animal();
   Dog dog = new Dog();
   Collie collie = new Collie();
   
   // 下面哪种用法是正确的
   extendsDogs.add(animal); // error
   extendsDogs.add(dog); // error
   extendsDogs.add(collie); // error
   
   superDogs.add(animal); // error
   superDogs.add(dog); // error
   superDogs.add(collie); // right
}

5. 上下界真正的作用

在多态中,如果父类是Food,子类是Fruit,那么父类的引用,新建子类是可行的。但是对于一个List,List<Food>和List<Fruit>就是两个完全无关的东西,没有办法相互引用。但是 List<T extend Food>就解决了这个问题。如下所示:

class Food{}
class Friut extends Food{}
class Apple extends Friut{}
Food food = new Food();
Friut friut = new Friut();
Apple apple = new Apple();
List<? extends Friut> list1 = new ArrayList<>();
List<? super Friut> list2 = new ArrayList<>();

1、list1.add(apple);  
2、List1.add(friut);
3、List1.add(food);

4、list2.add(apple);
5、List2.add(friut);
6、List2.add(food);

7、List<Food> foods = new ArrayList(); list1 = foods;
8、List<Friut> foods = new ArrayList(); list1 = foods;
9、List<Apple> foods = new ArrayList(); list1 = foods;

10、List<Food> foods = new ArrayList(); list2 = foods;
11、List<Friut> foods = new ArrayList(); list2 = foods;
12、List<Apple> foods = new ArrayList(); list2 = foods;


false false false 
true true false 
false true true 
true true false

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值