加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 包含同一超类的不同对象的ArrayList – 如何访问子类的

发布时间:2020-12-14 05:02:15 所属栏目:Java 来源:网络整理
导读:嗨,我想知道我的问题是否有一个简单的解决方案, 我有一个ArrayList: ArrayList Animal animalList = new ArrayListAnimal(); /* I add some objects from subclasses of Animal */animalList.add(new Reptile());animalList.add(new Bird());animalList.add
嗨,我想知道我的问题是否有一个简单的解决方案,

我有一个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() – 当调用move()时,Bird会飞.
我知道我可以通过使用它来访问超类的常用方法和属性

public void feed(Integer animalIndex) {
    Animal aAnimal = (Animal) this.animalList.get(animalIndex);
    aAnimal.eat();
}

那没关系 – 但现在我想访问子类Bird所具有的move()方法.
我可以通过将动物像鸟一样投射来做到这一点:

Bird aBird = (Bird) this.animalList.get(animalIndex);
aBird.move();

在我的情况下,我不想这样做,因为这意味着我有3个不同的上述代码集,每个子??类型为Animal.

这似乎有点多余,有更好的方法吗?

解决方法

从超类中确实没有很好的方法可以做到这一点,因为每个子类的行为都会有所不同.

要确保您实际调用适当的移动方法,请将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();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读