使用< ;?有什么区别?扩展SomeAbstract>与Java泛型中的SomeAbstract相比 [英] What is the difference between using <? extends SomeAbstract> vs. SomeAbstract in java generics
问题描述
我已经看到一些完全用列表与LT ;?扩展SomeAbstract>
与 List< ;? super SomeAbstract>
与 List< SomeAbstract>
,但我猜测在泛型中使用和不使用扩展。
这是真的吗?如果使用抽象类作为父类,答案会改变吗?
class My_AbstractExtends< T extends SomeAbstract>
vs。
class My_Abstract< SomeAbstract>
编辑 h2>
创建子类如下所示:
class My_ChildExtends extends My_AbstractExtends< ConcreteChildOfSomeAbstract>
vs。
class My_Child extends My_Abstract< ConcreteChildOfSomeAbstract>
我猜你在谈论使用在类型参数声明中扩展。在这种情况下:
class My_Abstract< T extends SomeAbstract>
有一个有界的类型参数,称为 T
必须是 SomeAbstract
或它的某个子类型。
class My_Abstract< SomeAbstract> ;
有一个无界的类型参数,称为 SomeAbstract
可以是任何东西。请注意, SomeAbstract
不再引用第一个示例使用的实际类型 SomeAbstract
!
为此,请想象如果第二个声明是 class My_Abstract< T>
。 T
显然是一个类型参数,而不是实际的类型。但它不必被称为 T
...它可以称为 E
或 Bob
或 SomeAbstract
。在所有这些情况下,它仍然只是一个类型参数......实际类型永远不会去那里,也不会对它有任何意义(类型参数的全部内容不是指特定类型,而是而是在创建类的实例时允许其他类型置于其位置)。
在您编辑的代码中,将 My_Child
的声明为
class My_Child extends My_Abstract< Object>
,你会看到不同之处。如果你真的试图在第二个版本中使用类型参数 SomeAbstract
来做某些事情,你还会发现你不能调用真正的 SomeAbstract
类。这就是为什么你总是遵循使用单字母类型参数的惯例的一个很好的例子......如果你不这样做,这真是令人困惑。
这是变得很长,但我也想指出,所有这些基本上与你的问题的前半部分无关。通配符像?扩展SomeAbstract
和? super SomeAbstract
不用于类型参数声明(例如定义泛型类时使用的声明),它们主要用于方法参数。 List
是解释为什么需要通配符的典型示例,因为它作为对象容器的性质使其相对容易理解,但与它们相关的规则适用于任何泛型类型。我试图用相对一般的术语在这个答案。
I'm moving over to java from DotNet and this idea of extends is new.
I've seen some posts that fully explain using List<? extends SomeAbstract>
vs. List<? super SomeAbstract>
vs. List<SomeAbstract>
, but I'm guessing that there is no difference between using, and not using, extends in generics.
Is that true? Would the answer change if using an abstract class as the parent?
class My_AbstractExtends<T extends SomeAbstract>
vs.
class My_Abstract<SomeAbstract>
EDIT
Creates child classes as follows
class My_ChildExtends extends My_AbstractExtends<ConcreteChildOfSomeAbstract>
vs.
class My_Child extends My_Abstract<ConcreteChildOfSomeAbstract>
I'm guessing you're talking about the use of extends in type parameter declarations. In that case:
class My_Abstract<T extends SomeAbstract>
has a bounded type parameter called T
that must be SomeAbstract
or some subtype of it.
class My_Abstract<SomeAbstract>
has an unbounded type parameter called SomeAbstract
that could be anything. Note that SomeAbstract
no longer refers to the actual type SomeAbstract
that the first example uses at all!
To expand on that: imagine if the second declaration were class My_Abstract<T>
. The T
there is clearly a type parameter, not an actual type. But it doesn't have to be called T
... it can be called E
or Bob
or SomeAbstract
. In all of these cases, it is still just a type parameter... an actual type can never go there, nor does it make any sense for it to (the whole point of a type parameter is to NOT refer to a specific type but instead allow other types to be put in its place when instances of the class are created).
In the code you've edited in, change My_Child
's declaration to
class My_Child extends My_Abstract<Object>
and you'll see the difference. If you actually try to do something using the type parameter SomeAbstract
in the second version, you'd also find that you cannot call any methods declared in the real SomeAbstract
class. This is all a good example of why you should always follow the convention of using single-letter type parameters... it's really confusing if you don't.
This is getting really long, but I also want to note that all of this is basically unrelated to the first half of your question. Wildcards like ? extends SomeAbstract
and ? super SomeAbstract
aren't used in type parameter declarations (such as those used when defining a generic class), they're primarily used for method parameters. List
is a typical example for explaining why wildcards are needed because its nature as a container of objects makes it relatively easy to understand, but rules pertaining to them apply to any generic type. I tried to explain this in relatively general terms in this answer.
这篇关于使用< ;?有什么区别?扩展SomeAbstract>与Java泛型中的SomeAbstract相比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!