Java 泛型(通配符) [英] Java Generics (Wildcards)
问题描述
我有几个关于 Java 中的通用通配符的问题:
I have a couple of questions about generic wildcards in Java:
List<有什么区别?扩展 T>
和List
?
什么是有界通配符,什么是无界通配符?
What is a bounded wildcard and what is an unbounded wildcard?
推荐答案
在你的第一个问题中, 和
是有界通配符的例子.无界通配符看起来像
>
,基本上意味着 .它松散地意味着泛型可以是任何类型.有界通配符(
extends T>
或 super T>
)通过说它必须extend 特定类型( 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).
The Java Tutorials have some pretty good explanations of generics in the articles Wildcards and More Fun with Wildcards.
这篇关于Java 泛型(通配符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!