List<Integer>
和List<? super Integer>
有什么区别?列表<Integer>和List <?之间的区别super Integer>
哪一个是好的做法或什么时候我们应该用什么?
List<Integer>
和List<? super Integer>
有什么区别?列表<Integer>和List <?之间的区别super Integer>
哪一个是好的做法或什么时候我们应该用什么?
List<Integer>
是List
即界到类型Integer
。这意味着它可以接收和生产Integer
。
List<? super Integer>
是一个无限List
接受那就是Integer
一个Integer
或超类的任何值。
第二个选项是最好的PECS使用原则(佩奇代表生产者延伸消费者超级)。如果您想要根据类型T
添加项目,则无需考虑它的实际类型。
欲了解更多信息,请参阅相关职位here。
List<Integer>
是这里最好的选择。
List<? super AbstractObject>
如果你在处理多态性问题,会是更好的选择。
最明显的区别是,你可以得到元素出List<Integer>
作为Integer
类型,但是当你的元素出来List<? super Integer>
,你只能得到型Object
。
在这种情况下'超级'工作吗? – 2013-05-07 08:55:17
@Marco Forberg,是的!取决于你想达到什么。 – 2013-05-07 08:55:49
可能的重复http://stackoverflow.com/q/3847162/1313143 – MarioDS 2013-05-07 08:56:37