java 所有实现接口的对象列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1460678/
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-29 16:37:37  来源:igfitidea点击:

java list of objects that all implement an interface

javagenerics

提问by Gerard

Suppose I have a class that implements an interface:

假设我有一个实现接口的类:

public class A implements IB

and I have a List<A>that I would like to reference to: List<? implements IB> list. So that I can code: for (IB item : list)etc.

我有一个List<A>我想参考的:List<? implements IB> list。这样我就可以编码:for (IB item : list)等。

Can it be done somehow in Java? There is no <? implements ...>possibility. What am I missing?

可以在 Java 中以某种方式完成吗?没有<? implements ...>可能。我错过了什么?

采纳答案by Paul Brinkley

If you meant that class A implements IA, rather than IB, then your code should be just fine saying

如果您的意思是 A 类实现 IA,而不是 IB,那么您的代码应该没问题

for (A item : list) {
    // handle item as if it is an IA
}

since all As are, by definition IAs.

因为根据定义,所有 As 都是 IAs。

Meanwhile, there is no wildcard for <? implements C>. There is <? extends C>, and Ccan be an interface or a class; however, this isn't necessary for what you seem to be trying to do.

同时, 没有通配符<? implements C>。有<? extends C>, 并且C可以是接口或类;但是,这对于您似乎正在尝试做的事情来说不是必需的。

If you want expressly to say for (IA item : list)because you're not guaranteeing that items in that list are As, but are guaranteeing that they are IAs, then I think you have a slight problem (I can't tell for sure, since you didn't say where this list processing code is located). A List<A>is nota List<IA>by definition; if you're building a List<A>and then passing it to a method that expects a List<IA>, you'll get a compile time error. You can, however, create a List<IA>and fill it with As. The reason for this is explained in Java's tutorial on generics.

如果你想明确地说,for (IA item : list)因为你不保证该列表中A的项目是s,但保证它们是IAs,那么我认为你有一个小问题(我不能肯定,因为你没有说这个列表处理代码所在的位置)。阿List<A>不是一个List<IA>由定义; 如果您正在构建 aList<A>然后将其传递给需要 a 的方法,List<IA>则会出现编译时错误。但是,您可以创建 aList<IA>并用As填充它。原因在 Java 的泛型教程中有解释。

回答by coobird

Use extends:

使用extends

public void someMethod(List<? extends IB> list) {
  for (IB e : list) {
    // Perform some processing on each element.
  }
}

public void anotherMethod() {
  List<A> list = new ArrayList<A>();
  someMethod(list);
}

More information can be found at The Java Tutorialswhich has a comprehensive lesson on Generics.

更多信息可以在The Java Tutorials 中找到,它有一个关于泛型综合课程

In particular, the section on wildcardsexplains how to use the ?with superand extendsto specify a parameter to be one that is a superclass or subclass of a given class.

特别是,通配符部分解释了如何使用?withsuper并将extends参数指定为给定类的超类或子类。

回答by OscarRyz

What about:

关于什么:

List<? extends IA> list;

回答by bruno conde

You can do it with extends:

你可以这样做extends

List<? extends IA> list

回答by Powerlord

Well, you could declare it a List<IA>.

好吧,您可以将其声明为List<IA>.

To build upon this further, List can store any objects that implement IA in it. The catch is that, when retrieving them back out, you don't know the original class without doing a .getClass()on it.

为了进一步构建,List 可以在其中存储任何实现 IA 的对象。问题是,在检索它们时,如果不对其进行任何操作,您将不知道原始类.getClass()

Example:

例子:

List<IA> list = new ArrayList<IA>();
// code to populate list here
for (IA item : list) {
    item.iaMethod();
}