java 集合<是什么意思?扩展 EmpApp>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3558587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:24:22  来源:igfitidea点击:

What is mean by Collection<? extends EmpApp>?

javagenericscollections

提问by Nate W.

In my code i am using two different class objects empobj& employee. Eclipse asks to change the code

在我的代码中,我使用了两个不同的类对象empobj员工。Eclipse 要求更改代码

empobj.addAll(employee);

to

empobj.addAll((Collection<? extends EmpApp>) employee);

What does it mean? I cannot understand the concept here. Can I get any clarifications about this?

这是什么意思?我无法理解这里的概念。我能得到任何澄清吗?

回答by mnuzzo

Collection<? extends EmpApp>

Is a definition of a collection. The "employee into a collection which contains objects of a class which extends EmpApp. The "?" in the generic means that Eclipse isn't sure what classname should be there.

是集合的定义。“员工进入一个包含扩展 EmpApp 的类的对象的集合中。”泛型中的“?”意味着 Eclipse 不确定应该在那里使用什么类名。

For more on generics in Java, here's the wikipedia article

有关 Java 中泛型的更多信息,请参阅维基百科文章

回答by Nate W.

addAlltakes as its parameter a Collectionand then adds all the elements of that collection to itself. In your case, you're trying to give it something that is not a Collection, so the compiler is trying to make it work by casting the argument to the correct type. In this particular case, the correct type is a collection of some object which extends EmpApp, i.e., Collection<? extends EmpApp>.

addAll将其作为参数 a Collection,然后将该集合的所有元素添加到自身中。在你的情况下,你试图给它一些不是 a 的东西Collection,所以编译器试图通过将参数转换为正确的类型来使其工作。在这种特殊情况下,正确的类型是某个扩展对象的集合EmpApp,即Collection<? extends EmpApp>

Judging from the name of your variable, employeeprobably isn't a collection, so you need to revisit the API for whatever collection empobjis and find out how you can add a single element to it (likely add).

从变量的名称来看,employee可能不是集合,因此您需要重新访问 API 以了解任何集合empobj,并找出如何向其中添加单个元素(可能add)。

回答by user2556262

A collection is group of object to be treated as a single unit.Arbitrary objects can be stored,retrieved,& manipulated as elements of collection.

集合是一组对象,被视为一个单元。任意对象可以作为集合的元素进行存储、检索和操作。