您好我想知道我的问题是否有简单的解决方案,
我有ArrayList
:
ArrayList <Animal> animalList = new ArrayList<Animal>();
/* I add some objects from subclasses of Animal */
animalList.add(new Reptile());
animalList.add(new Bird());
animalList.add(new Amphibian());
他们都实施了一种方法move()
- Bird
在调用move()
时过得很快。
我知道我可以使用这个
public void feed(Integer animalIndex) {
Animal aAnimal = (Animal) this.animalList.get(animalIndex);
aAnimal.eat();
}
没关系 - 但现在我想访问子类move()
具有的Bird
方法。
我可以将Animal
转换为Bird
:
Bird aBird = (Bird) this.animalList.get(animalIndex);
aBird.move();
在我的情况下,我不想这样做,因为这意味着我为Animal
的每个子类型提供了上述代码的3组不同。
似乎有点多余,有更好的方法吗?
答案 0 :(得分:12)
从超类中确实没有很好的方法可以做到这一点,因为每个子类的行为都会有所不同。
要确保您实际调用适当的move
方法,请将Animal
从超类更改为接口。然后,当您调用move
方法时,您将能够确保为所需对象调用适当的移动方法。
如果您希望保留公共字段,那么您可以定义一个抽象类AnimalBase
,并要求所有动物构建它,但每个实现都需要实现Animal
接口
示例:
public abstract class AnimalBase {
private String name;
private int age;
private boolean gender;
// getters and setters for the above are good to have here
}
public interface Animal {
public void move();
public void eat();
public void sleep();
}
// The below won't compile because the contract for the interface changed.
// You'll have to implement eat and sleep for each object.
public class Reptiles extends AnimalBase implements Animal {
public void move() {
System.out.println("Slither!");
}
}
public class Birds extends AnimalBase implements Animal {
public void move() {
System.out.println("Flap flap!");
}
}
public class Amphibians extends AnimalBase implements Animal {
public void move() {
System.out.println("Some sort of moving sound...");
}
}
// in some method, you'll be calling the below
List<Animal> animalList = new ArrayList<>();
animalList.add(new Reptiles());
animalList.add(new Amphibians());
animalList.add(new Birds());
// call your method without fear of it being generic
for(Animal a : animalList) {
a.move();
}
答案 1 :(得分:1)
你不需要做任何演员。被重写的方法应该被称为[simple polymorphism]
Animal aAnimal== this.animalList.get(animalIndex);
aAnimal.move();
如果对象是鸟,上面的代码应该调用bird方法,不是吗?
铸造不是解决方案,你将如何决定投射哪个对象?您将不得不使用instanceOf。
答案 2 :(得分:0)
在您的情况下,以下可能有效,但时间复杂度为O(n):
public void moveBird(){
for(Animal aminal:animalList){
if(animal instanceof Bird){
aninmal.move();
}
}
}
答案 3 :(得分:-1)
Bird getMyBird(Integer aniInteger) {
Bird b = new Bird();
//Do somthig with bird object...
return b;
//get your modifeid bird object
}
Bird myBird = animalList.get(animalIndex);
myBird.move();