[英]Java — How to cleanly constuct lists of subclasses from a list of the superclass?
Specifically, I have a list of objects. 具体来说,我有一个对象列表。 Each subclass of that class of objects is associated with a specific piece of functionality.
该类对象的每个子类都与特定的功能相关联。
Here is my naive solution to this problem, and it is ugly: 这是我对这个问题的天真解决方案,它很难看:
List<SuperClass> superClasses = ...
List<Subclass1> obj1 = Lists.newArrayList();
List<Subclass1> obj2 = Lists.newArrayList();
List<Subclass1> obj3 = Lists.newArrayList();
List<Subclass1> obj4 = Lists.newArrayList();
...
for (SuperClass obj : superClasses) {
if (obj.getClass().equals(Subclass1.class)) {
obj1.add((Subclass1) obj);
}
}
//repeat for each subclass
I also tried something like this: 我也尝试过这样的事情:
public Map<Class, List<SuperClass>> constructMap(List<SuperClass> superClasses);
but this couldn't be used: 但这不能用:
(List<Subclass1>) constructMap(superClasses).get(Subclass1.class)
cannot cast from List<SuperClass> to List<Subclass1>.
Am I doomed to the naive code here? 我在这里注定了天真的代码吗? Is there really no smart way to take in a list of super-class objects and handle them based on their actual class?
真的没有聪明的方法来接受超类对象列表并根据它们的实际类来处理它们吗?
Here is the problem I am trying to solve: 这是我想要解决的问题:
public Driver {
List<Fruit> fruits = collectAllFruit();
StemHandler.pullStem(fruitsThatAreApples, fruitsThatArePears);
ColorHandler.getColor(fruitsThatAreApples, fruitsThatAreOranges, fruitsThatArePears);
public interface Fruit {
getColor();
}
public class Apple implements Fruit {
getColor();
pullStem();
}
public class Orange implements Fruit {
getColor();
peel();
}
public class Pear implements Fruit {
getColor();
pullStem();
}
public interface FruitHandler {
}
public class StemHandler extends FruitHandler {
pullStem(List<Apple>, List<Pear>)
}
public class ColorHandler extends FruitHandler {
getColor(List<Apple>, List<Orange>, List<Pear>)
}
The correct thing is to use polymorphism using your list of the base type ( List<SuperClass>
). 正确的做法是使用基类型
List<SuperClass>
( List<SuperClass>
)来使用多态。 You shouldn't handle them differently in your loop that iterates through the different sub classes. 您不应该在遍历不同子类的循环中以不同方式处理它们。 What you should do is call a specific method that is defined on the super class (better yet, an interface) and implement the method differently in each sub class (implementation).
你应该做的是调用在超类(更好的是,接口)上定义的特定方法,并在每个子类(实现)中以不同方式实现该方法。 See Returning an extended class
请参阅返回扩展类
interface MyInteface{
public void doSomething();
}
class Sub1 implements MyInterface{
public void doSomething() {
System.out.println("Behavior # 1");
}
}
class Sub2 implements MyInterface{
public void doSomething() {
System.out.println("Different Behavior");
}
}
for (MyInteface obj : superClasses) {
// Will do different things for different implementations
obj.doSomething();
}
The secret is to define what are the common aspects of the different implementations and be able to squeeze them all into the same signature. 秘诀是定义不同实现的共同方面,并能够将它们全部压缩到相同的签名中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.