[英]Managing Abstract subclass within a generic List in Java
Generics and abstracts are mind boggling difficult to deal with, so bear with me while I try my best to explain the problem in a simple manner. 泛型和摘要很难处理,因此在我尽力以简单的方式解释问题时请多多包涵。
I have the following classes: 我有以下课程:
public class Community<T extends Person> {
List<T> people;
public void add(T person) {
people.add(person);
}
public List<T> getPeople() {
return people;
}
}
public abstract class Person {}
public class Aussie extends Person {}
Here is the code to instantiate a community of Aussies: 这是实例化澳大利亚社区的代码:
Aussie aus1 = new Aussie();
Aussie aus2 = new Aussie();
Community<Aussie> aussieCommunity = new Community<>();
aussieCommunity.add(aus1);
aussieCommunity.add(aus2);
Now let's take a step further and say that I have multiple communities that I wish to systematically store inside a list as follow: 现在让我们更进一步,说我有多个社区,希望将它们系统地存储在列表中,如下所示:
List<Community<?>> communities;
I hope you're still with me because here is my problem: 我希望您仍然与我在一起,因为这是我的问题:
I need to write a code that will take the list of community and display each person's details - assuming each person's details will be accessed differently in their own class. 我需要编写代码来获取社区列表并显示每个人的详细信息-假设每个人的详细信息在各自的班级中都将以不同的方式访问。 Example: Aussie may say "Oi" as hi, American's may say "Hello" as hi.
示例:澳大利亚人可能会说“ Oi”为嗨,美国人可能会说“ Hello”为嗨。
for (Community<?> community : communities) {
// I don't know what the type of community this is so, I use wildcard:
List<? extends Person> people = community.getPeople();
for (Type person : people) { // How do I specify the type of person eg Aussie/American etc here?
// Do something
}
}
Any suggestion on how I can specify the type of person in the second for loop? 关于如何在第二个for循环中指定人员类型的任何建议?
Ok. 好。 Here is an small example of how it can be done:
这是一个如何完成的小例子:
public abstract class Person {
public final String say(String sentance) {
StringTokenizer tokenizer = new StringTokenizer(sentance);
StringBuilder sb = new StringBuilder();
while (tokenizer.hasMoreTokens()) {
String word = tokenizer.nextToken();
String slang = getSlang(word);
sb.append(slang != null ? slang : word);
sb.append(tokenizer.hasMoreTokens() ? " " : "");
}
return sb.toString();
}
private String getSlang(String word) {
return getSlangMap().get(word);
}
protected abstract Map<String, String> getSlangMap();
}
public class Aussi extends Person {
@Override
protected Map<String, String> getSlangMap() {
Map<String, String> slangMap = new HashMap<>();
slangMap.put("hi", "Oi");
slangMap.put("there", "theeer");
return slangMap;
}
}
public class Swede extends Person {
@Override
protected Map<String, String> getSlangMap() {
Map<String, String> slangMap = new HashMap<>();
slangMap.put("hi", "hejsan");
slangMap.put("there", "där");
return slangMap;
}
}
public class CommunityTest {
@Test
public void testSayHiThere() throws Exception {
Aussi au1 = new Aussi();
Aussi au2 = new Aussi();
Community<Aussi> aussiCommunity = new Community<>();
aussiCommunity.add(au1);
aussiCommunity.add(au2);
Swede sw1 = new Swede();
Swede sw2 = new Swede();
Community<Swede> swedeCommunity = new Community<>();
swedeCommunity.add(sw1);
swedeCommunity.add(sw2);
List<Community<? extends Person>> communities = new ArrayList<>();
communities.add(aussiCommunity);
communities.add(swedeCommunity);
for (Community<? extends Person> community : communities) {
for (Person person : community.getPeople()) {
System.out.println(person.say("hi there"));
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.