Java 中通配符的工作原理

发布于 2024-11-03 12:53:19 字数 808 浏览 0 评论 0原文

我正在阅读有关泛型通配符的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白日梦 2024-11-10 12:53:19

你几乎完全颠倒了。

Collection可以包含 Object 及其子类,并且由于所有内容(包括 String)都是 Object< /code>,您可以将任何内容添加到这样的集合中。但是,您不能对其内容做出任何假设,除非它们是 Object

另一方面,Collection 仅包含特定未知类型(及其子类)的实例,但由于您不知道是哪个 类型就是这样,您不能向这样的集合添加任何内容(null 除外),也不能对其内容做出任何假设(除非它们是 Object) ,因为一切都是)。

You got it almost exactly backwards.

A Collection<Object> can contain Object and subclasses of it, and since everything (including String) is a subclass of Object, you can add anything to such a collection. However, you cannot make any assumptions about its contents except that they're Objects.

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 (except null) to such a collection, nor make any assumptions about its conents (except that they're Objects, because everything is).

忱杏 2024-11-10 12:53:19

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<?> and Collection<Object> are almost equivalent.

我不在是我 2024-11-10 12:53:19

在第二个语句的情况下,“?”通配符表示泛型未定义。结果是类型绑定到“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文