Java:将不同类型的对象放入 ModelCollection 的 ArrayList 中。接口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3707097/
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
Java: Put objects of different types into a ArrayList for a ModelCollection. Interfaces?
提问by OneWorld
I want to have my models wrapped up in a ModelCollection which is mostly used for a ListView. The Collection has always the same attributes like title, totalResults (for Pagination) and it shall contain the listItem-Models in the ArrayList "items". However, these models have different types like "ModelCategory" or "ModelChain" and often have different Properties and Methods.
我想让我的模型包含在一个主要用于 ListView 的 ModelCollection 中。集合始终具有相同的属性,如标题、totalResults(用于分页),并且它应包含 ArrayList“项目”中的 listItem-Models。但是,这些模型具有不同的类型,例如“ModelCategory”或“ModelChain”,并且通常具有不同的属性和方法。
How can I achieve this in java with strong Typing discipline? I heart interface a the right way to do this. WHERE do I have to implement them?
如何在具有强大打字规则的 Java 中实现这一目标?我的心接口是做到这一点的正确方法。我必须在哪里实施它们?
public class ModelCollection {
public ArrayList< ModelCategory OR ModelChain OR ModelXyz> items = new ArrayList<ModelCategory OR ModelChain OR ModelXyz>();
private String id;
private String title;
private Long updated;
private String linkSelf;
private int totalResults;
private int startIndex;
/*
more stuff like parsing a feed
*/
}
回答by Tauren
Make your ModelCategory, ModelChain, and ModelXyz implement an interface. Then have your collection be on that interface.
使您的 ModelCategory、ModelChain 和 ModelXyz 实现一个接口。然后让你的收藏在那个界面上。
For instance:
例如:
public interface MyModel {
}
public class ModelCategory implements MyModel {
}
List<MyModel> list = new ArrayList<MyModel>();
To reference specific methods of each class, you will need to cast your list objects to the correct type.
要引用每个类的特定方法,您需要将列表对象强制转换为正确的类型。
List<MyModel> list = new ArrayList<MyModel>();
list.add(new ModelCategory());
ModelCategory model = (ModelCategory) list.elementAt(0);
Obviously, you can use whatever methods you need to iterate through your collections.
显然,您可以使用任何您需要的方法来遍历您的集合。
回答by Jan Wegner
Solution of Tauren is correct, but remember to check instanceof like deadsven proposed, and the result is like:
牛头人的解决方案是正确的,但记得像deadsven提出的那样检查instanceof,结果是这样的:
List<MyModel> list = new ArrayList<MyModel>();
list.add(new ModelCategory());
for(MyModel mymodelListElement: list) {
//mymodelListElement.sameMyModelMethods()
if(ModelCategory instanceof mymodelListElement) {
ModelCategory modelCategoryElement = (ModelCategory)mymodelListElement;
} else if(ModelChain instanceof mymodelListElement) {
ModelChain modelChainElement = (ModelChain )mymodelListElement;
} else if(ModelXyz instanceof mymodelListElement) {
ModelXyz modelXyzElement = (ModelXyz)mymodelListElement;
} else {
//ignore
//or
//throw new RuntimeException("Wrong MyModel implementation")
}
}
回答by willcodejavaforfood
Define a common interface for all your model classes which gives access to the properties they share.
为所有模型类定义一个通用接口,以便访问它们共享的属性。
public interface MooModel
String getTitle();
// etc
And define the List<MooModel>
并定义 List<MooModel>