Java 中通配符的工作原理
我正在阅读有关泛型通配符的java教程。在下面的代码中:
void printCollection(Collection<Object> c) {
for (Object e : c) {
System.out.println(e);
}
}
这是否意味着集合c
将类型object
作为其元素,并且我们不能调用c.add(“apple”), 因为“apple”是一个字符串,并且 for 循环从集合
c
中获取任何 object
元素?
但我不明白下面的代码,
void printCollection(Collection<?> c) {
for (Object e : c) {
System.out.println(e);
}
}
该代码使用通配符,意思是“元素类型与任何内容匹配的集合”。这是否意味着我们可以向其中添加任何类型的对象,例如c.add("string");
, c.add(1);
和 c.add(new apple());
? for 循环从集合 c
中获取任何对象 e
,如果 c
不是 object
类型,我们说c
的元素是 Integer。这段代码有效吗?这是否意味着它应该被铸造?
I am reading the java tutorial about Wildcards in Generics. In the following code:
void printCollection(Collection<Object> c) {
for (Object e : c) {
System.out.println(e);
}
}
Does this means the collection c
take type object
as its elements, and we can not call c.add("apple")
,
because "apple" is a string and the for loop takes any object
elements from collection c
?
But I do not understand the following code,
void printCollection(Collection<?> c) {
for (Object e : c) {
System.out.println(e);
}
}
This code uses wildcards, meaning "a collection whose element type matches anything." Does this mean we can add any type of object to it, such as c.add("string");
,c.add(1);
, and c.add(new apple());
?
and the for loop take any object e
from collection c
, if c
is not an object
type, we say c
's elements are Integer. Does this code works? Does this mean it should be cast?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你几乎完全颠倒了。
Collection
另一方面,
Collection
仅包含特定未知类型(及其子类)的实例,但由于您不知道是哪个 类型就是这样,您不能向这样的集合添加任何内容(null
除外),也不能对其内容做出任何假设(除非它们是Object
) ,因为一切都是)。You got it almost exactly backwards.
A
Collection<Object>
can containObject
and subclasses of it, and since everything (includingString
) is a subclass ofObject
, you can add anything to such a collection. However, you cannot make any assumptions about its contents except that they'reObject
s.On the other hand, A
Collection<?>
contains only instances of a specific unknown type (and its subclasses), but since you don't know which type it is, you cannot add anything (exceptnull
) to such a collection, nor make any assumptions about its conents (except that they'reObject
s, because everything is).在 Angelika Langer 的 Java 泛型常见问题解答中,问题“无界通配符参数化类型之间有什么区别和原始类型?” (链接)您会看到
Collection ;
和Collection
In Angelika Langer's Java Generics FAQ, question "What is the difference between the unbounded wildcard parameterized type and the raw type?" (link) you'll see that
Collection<?>
andCollection<Object>
are almost equivalent.在第二个语句的情况下,“?”通配符表示泛型未定义。结果是类型绑定到“Object”,因为这是无声明的默认绑定。
事实上,甚至“Integer”也是 Object 的子类。如果你的意思是“int”,你是对的,那是一个基元,而不是对象的派生,但你不能将它放入集合中,因为集合只允许对象的派生。
对于放入集合中的元素是否应该进行强制转换的问题。不,那不是必需的,因为它们是 Object 的明确派生类。编译器不需要任何显式的强制转换信息,它会自动解析正确的类定义。
In the case of the second statement the "?" wildcard means that the generic is not defined. The result is that the type is bound to "Object" because this is the default bound for no declaration.
In fact even "Integer" is a subclass of Object. If you mean "int" you are right, thats a primitive and not a derivate of Object, but you can't put it into a Collection, since a Collection only allows derivates of Object.
And to the question if the elements put into the collection should be casted. No, thats not necessary since they are clear derivate classes from Object. The compiler does not need any explicit cast information there, he resolves the right class definition automatically.