Java泛型(通配符) [英] Java Generics (Wildcards)
问题描述
在Java中我有几个关于泛型通配符的问题:
$ b
I have a couple of questions about generic wildcards in Java:
-
列表与LT ;?延伸T>
和列表< ;? super T>
?
什么是有界通配符?什么是无界通配符? b $ b
What is a bounded wildcard and what is an unbounded wildcard?
推荐答案
在第一个问题中,<扩展T>
和<? super T>
是有界通配符的例子。无界通配符看起来像<?>
,并且基本上意味着< ;?扩展对象>
。它松散地表示泛型可以是任何类型。有界通配符(< ;? extends T>
或<?super T>
)对类型通过说它要么扩展特定类型(< ;? extends T>
被称为上限),要么必须是一个特定类型的祖先(< ;? super T>
被称为下限)。
In your first question, <? extends T>
and <? super T>
are examples of bounded wildcards. An unbounded wildcard looks like <?>
, and basically means <? extends Object>
. It loosely means the generic can be any type. A bounded wildcard (<? extends T>
or <? super T>
) places a restriction on the type by saying that it either has to extend a specific type (<? extends T>
is known as an upper bound), or has to be an ancestor of a specific type (<? super T>
is known as a lower bound).
Java教程在通配符中对泛型有一些非常好的解释, a>和更多有趣的通配符。
The Java Tutorials have some pretty good explanations of generics in the articles Wildcards and More Fun with Wildcards.
这篇关于Java泛型(通配符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!