[英]java bounded generic type definition
What's the difference between the following type definitions 以下类型定义之间的区别是什么
<E extends Number>
and 和
<? extends Number>
Cheers, Don 干杯,唐
This version: 这个版本:
<? extends Number>
can appear in a non-generic method/type, and it basically means "I don't care what the type is, so long as it derives from Number
. I'm not going to really use the type, I just need it to be appropriate." 可以出现在非泛型方法/类型中,它基本上意味着“我不关心类型是什么,只要它来自
Number
。我不会真正使用该类型,我只需要它合适。“
This version: 这个版本:
<E extends Number>
requires E to be a type parameter. 要求E是一个类型参数。 It allows you to do more (for instance, creating an
ArrayList<E>
later on) but the extra type parameter can make things more complicated when you don't really need them to be. 它允许你做更多的事情(例如,稍后创建一个
ArrayList<E>
),但额外的类型参数可以使事情变得更复杂,当你真的不需要它们时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.