Java泛型中的PECS原则

先来看一个错误:

List<? extends Foo> list1 = new ArrayList<Foo>();
List<? extends Foo> list2 = new ArrayList<Foo>();

 /* Won't compile */
 list2.add( new Foo() ); //error 1
 list1.addAll(list2);    //error 2 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

error 1:

IntelliJ says:

add(capture<? extends Foo>) in List cannot be applied to add(Foo)
 
 
  • 1
  • 1

The compiler says:

cannot find symbol
symbol  : method addAll(java.util.List<capture#692 of ? extends Foo>)
location: interface java.util.List<capture#128 of ? extends Foo>
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

error 2:

IntelliJ gives me

addAll(java.util.Collection<? extends capture<? extends Foo>>) in List cannot be applied to addAll(java.util.List<capture<? extends Foo>>)
 
 
  • 1
  • 1

Whereas the compiler just says

cannot find symbol
symbol  : method addAll(java.util.List<capture#692 of ? extends Foo>)
location: interface java.util.List<capture#128 of ? extends Foo>
        list1.addAll(list2);
 
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

PECS法则

在看完下面简短的PECS后,再去思考思考上面的问题。

我们知道Java泛型可以有多种写法,主要是 extends 和 super 关键字。比如:

HashMap< T extends String>;
HashMap< ? extends String>;
HashMap< T super String>;
HashMap< ? super String>;
 
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

主要涉及的是Java泛型中重要的PECS法则:

? extends

List<Apple> apples = new ArrayList<>();
List<? extends Fruit> fruits = apples; //works, apple is a subclass of Fruit.
fruits.addAll(apples);            //compile error
fruits.add(new Strawberry());     //compile error
 
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

存入数据:

  • fruits是一个Fruit子类的List,由于Apple是Fruit的子类,因此将apples赋给fruits是合法的。

  • 编译器会阻止将Strawberry类加入fruits。在向fruits中添加元素时,编译器会检查类型是否符合要求。因为编译器只知道fruits是Fruit某个子类的List,但并不知道这个子类具体是什么类,为了类型安全,只好阻止向其中加入任何子类。

  • 那么可不可以加入Fruit呢?很遗憾,也不可以。事实上,不能往一个使用了? extends数据结构里写入任何的值。

疑问:向fruits中添加元素要检查类型,由于不知道具体的子类,编译报错。但为什么apples使用“=”赋给fruits就可以??应该由于不知道具体子类,也会编译报错才对啊

读取数据

但是,由于编译器知道它总是Fruit的子类型,因此我们总可以从中读取出Fruit对象:

Fruit fruit = fruits.get(0);
 
 
  • 1
  • 1

? super

List<Fruit> fruits = new ArrayList<Fruit>();
List<? super Apple> apples  = fruits;
apples .add(new Apple());                 //work
apples .add(new RedApple());              //work
apples .add(new Fruit());                 //compile error 
apples .add(new Object());                //compile error
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

存入数据:

  • fruits是一个Apple超类(父类,superclass)的List。

  • 出于对类型安全的考虑,我们可以加入Apple对象或者其任何子类(如RedApple)对象(因为编译器会自动向上转型),但由于编译器并不知道List的内容究竟是Apple的哪个超类,因此不允许加入特定的任何超类型。

读取数据

编译器在不知道是什么类型的情况下只能返回Object对象,因为Object是任何Java类的最终祖先类。

Object fruit = apples.get(0);
 
 
  • 1
  • 1

PECS原则总结

从上述两方面的分析,总结PECS原则如下:

  • 如果要从集合中读取类型T的数据,并且不能写入,可以使用 ? extends 通配符;(Producer Extends)
  • 如果要从集合中写入类型T的数据,并且不需要读取,可以使用 ? super 通配符;(Consumer Super)
  • 如果既要存又要取,那么就不要使用任何通配符。

现在再去思考最开始的问题,应该会更清楚一点.



=========================

转自:http://blog.csdn.net/xx326664162/article/details/52175283

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值