In Java type arguments, does <? extends E> mean strictly subtypes only? or would E also suffice?

白昼怎懂夜的黑 提交于 2019-12-06 23:47:10

问题


In Java type arguments, does mean strictly subtypes only? or would E also suffice?


回答1:


Yes, super and extends gives inclusive lower and upper bounds respectively.

Here's a quote from Angelika Langer's Generics FAQ:

What is a bounded wildcard?

A wildcard with an upper bound looks like ? extends Type and stands for the family of all types that are subtypes of Type , type Type being included. Type is called the upper bound.

A wildcard with a lower bound looks like ? super Type and stands for the family of all types that are supertypes of Type , type Type being included. Type is called the lower bound.




回答2:


It's not strict; E would suffice.




回答3:


List<? extends Animal> animalList=new List<Dog>();
List<? extends Animal> animalList=new List<Animal>();

Both the lines compile without any error. Any function taking the list as a parameter understands that the objects in the list are of type E or a subtype of E.



来源:https://stackoverflow.com/questions/3136680/in-java-type-arguments-does-extends-e-mean-strictly-subtypes-only-or-would

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!