如何在Java中创建动物列表

时间:2018-06-23 12:31:39

标签: java arraylist polymorphism

    public class Animaltestdrive{
    public static void main(String args[]){
     MyanimalList list=new MyanimalList();
     Dog a=new Dog();
     Cat c=new Cat();
     list.add(a);
     list.add(c);
 }}
public class MyanimalList{
    private Animal[] animals = new Animal[5];
    private int nextIndex=0;

    public void add(Animal a){
        if(nextIndex< animals.length){
            animals[nextIndex]=a;
            System.out.println("Animal added at " + nextIndex);
            nextIndex++;
        }
    }
}

我希望对此进行修改,因此应提供输出: 动物添加为0 在1点添加动物

0 个答案:

没有答案