gpt4 book ai didi

java - 在 ArrayList 中存储不同类的对象

转载 作者:行者123 更新时间:2023-11-30 06:14:11 26 4
gpt4 key购买 nike

我有一个抽象的动物类和其他子类,如爬行动物,它们被进一步继承。
我创建了数组来初始化动物,如下所示:

public void initializeArray()
{
zooAnimal = new Animal[10]; // We will make 10 animals:

// Polymorphism allows us to assign an object of a subclass to an
// object reference to a superclass/ancestor.
zooAnimal[0] = new Kookaburra("Kelly",5); // A 5kg kookaburra
zooAnimal[1] = new Lizard("Lizzy",2,3); // A 2kg, 3-year-old lizard
zooAnimal[2] = new Crocodile("Colin", 200, 7); // a 7-yo 200kg croc.
zooAnimal[3] = new Rosella("Katie", 2, "Crimson"); // a 2-yo Crimson Rosella
zooAnimal[4] = new Rosella("Rosie", 4, "Green"); // a 4-yo Green Rosella
zooAnimal[5] = new Snake("Boris","Brown",15,3); // A Brown Snake, 15kg, 3 years
zooAnimal[7] = new Snake("Rita","Rattle",22,1); // A Rattle Snake, 22kg, 1 years
zooAnimal[6] = new Dolphin("Dolly", 142, 6); // A heavy, 6-yo dolphin.
zooAnimal[8] = new Kookaburra("Kenneth",4); // A 4kg kookaburra
zooAnimal[9] = new Rosella("Yippy", 1, "Yellow"); // a 1-yo Yellow Rosella
}

但我想使用 ArrayList 而不是数组来实现相同的效果。
如何做到这一点?

我的 Animal 类和子类如下所示:

动物类

public abstract class Animal
{
private int weight;
private int age;
private String name;

protected Animal(String name, int weight, int age)
{
this.name = name;
this.weight = weight;
this.age = age;
}

public final int getWeight() { return weight; }

public final int getAge() { return age; }

public final String getName() { return name; }

public abstract void makeNoise(); // Must be implemented by a subclass

/** Provides a default description of the animal.
* Sub-classes should override. */
public String toString()
{
return "Animal Object: [name=" + name + ", age=" + age + ", weight=" + weight + "]";
}
}

我有一个 Bird 类(Animal 类的子类),一个 Kookabura 类( 的子类) Animal)、Reptile 类(Animal 类的子类)和一个Lizard 子类( 的子类)爬虫类)等等!!

最佳答案

您只需要声明一个 ArrayList<Animal> , 并使用 add(Animal)方法而不是赋值(多态允许你这样做):

private ArrayList<Animal> zooAnimals;

public void initializeArray() {
// this 10 is optional, but it's good to specify it
// when you know the final length of your list in advance
zooAnimals = new ArrayList<>(10);

zooAnimals.add(new Kookaburra("Kelly", 5)); // A 5kg kookaburra
zooAnimals.add(new Lizard("Lizzy", 2, 3)); // A 2kg, 3-year-old lizard
zooAnimals.add(new Crocodile("Colin", 200, 7)); // a 7-yo 200kg croc.
zooAnimals.add(new Rosella("Katie", 2, "Crimson")); // a 2-yo Crimson Rosella
zooAnimals.add(new Rosella("Rosie", 4, "Green")); // a 4-yo Green Rosella
zooAnimals.add(new Snake("Boris", "Brown", 15, 3)); // A Brown Snake, 15kg, 3 years
zooAnimals.add(new Snake("Rita", "Rattle", 22, 1)); // A Rattle Snake, 22kg, 1 years
zooAnimals.add(new Dolphin("Dolly", 142, 6)); // A heavy, 6-yo dolphin.
zooAnimals.add(new Kookaburra("Kenneth", 4)); // A 4kg kookaburra
zooAnimals.add(new Rosella("Yippy", 1, "Yellow")); // a 1-yo Yellow Rosella
}

关于java - 在 ArrayList 中存储不同类的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30689975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com