如何向包含继承的 ArrayLists 添加属性?
How to add properties to ArrayLists that include inheritance?
我正在做一个关于继承的作业,到目前为止我已经创建了一个 superclass a subclass。在这些 classes 中,添加了一些方法来定义信息,例如动物的名字或年龄。现在我被要求做以下事情:
- 使用创建 Animal 对象的 ArrayList 的主要方法创建演示 class。用不同的动物填满列表,还有不同的名字和年龄。
我对此完全感到困惑。如果我尝试在我的新 ArrayList 中创建动物,它会告诉我 Animal class 是抽象的,无法实例化。下面是相关classes的内容:
动物class(超级class)
abstract public class Animal
{
int age;
String name;
String noise;
Animal(String name, int age)
{
this.age = age;
this.name = name;
}
Animal()
{
this("newborn", 0);
}
abstract public void makeNoise();
public String getName() {
return name;
}
public int getAge()
{
return age;
}
public void setName(String newName) {
name = newName;
}
abstract public Food eat(Food x) throws Exception;
abstract public void eat(Food food, int count) throws Exception;
}
狼class(子class)
import java.util.ArrayList;
public class Wolf extends Carnivore
{
ArrayList<Food> foodGroup = new ArrayList<>();
String name;
int age;
Wolf(String name, int age)
{
this.name = name;
this.age = age;
}
Wolf()
{
super();
}
public void makeNoise()
{
noise = "Woof!";
}
public String getNoise()
{
return noise;
}
public Food eat(Food x) throws Exception
{
if (x instanceof Meat) {
return x;
} else {
throw new Exception("Carnivores only eat meat!");
}
}
public void eat(Food food, int count) {
while (count > 0) {
addFood(food);
count--;
}
}
public void addFood(Food inFood)
{
foodGroup.add(inFood);
}
}
演示 class
import java.util.ArrayList;
public class Demo {
public static void main(String[] args)
{
ArrayList<Animal> animalGroup = new ArrayList<>();
//Add new Animals with properties such as name and age?
Animal wolf1 = new Wolf();
addAnimal(new Wolf("lnb1g16", 6));
}
public static void addAnimal(Animal inAnimal)
{
animalGroup.add(inAnimal);
}
}
显然,我想在演示 class 中基于这些先前的 classes 创建一组动物?我不明白这是如何完成的,也不知道为什么我需要创建另一个主要方法。任何关于如何编写演示 class 的帮助将不胜感激,因为我对我被要求做的事情感到困惑,谢谢。
演示class
public static void main(String[] args)
{
ArrayList<Animal> animalGroup = new ArrayList<>();
//Add new Animals with properties such as name and age?
Animal wolf1 = new Wolf();
animalGroup.add(new Wolf("sam", 5));
animalGroup.add(new Wolf("george", 5));
animalGroup.add(new Wolf("patrick", 7));
}
我正在做一个关于继承的作业,到目前为止我已经创建了一个 superclass a subclass。在这些 classes 中,添加了一些方法来定义信息,例如动物的名字或年龄。现在我被要求做以下事情:
- 使用创建 Animal 对象的 ArrayList 的主要方法创建演示 class。用不同的动物填满列表,还有不同的名字和年龄。
我对此完全感到困惑。如果我尝试在我的新 ArrayList 中创建动物,它会告诉我 Animal class 是抽象的,无法实例化。下面是相关classes的内容:
动物class(超级class)
abstract public class Animal
{
int age;
String name;
String noise;
Animal(String name, int age)
{
this.age = age;
this.name = name;
}
Animal()
{
this("newborn", 0);
}
abstract public void makeNoise();
public String getName() {
return name;
}
public int getAge()
{
return age;
}
public void setName(String newName) {
name = newName;
}
abstract public Food eat(Food x) throws Exception;
abstract public void eat(Food food, int count) throws Exception;
}
狼class(子class)
import java.util.ArrayList;
public class Wolf extends Carnivore
{
ArrayList<Food> foodGroup = new ArrayList<>();
String name;
int age;
Wolf(String name, int age)
{
this.name = name;
this.age = age;
}
Wolf()
{
super();
}
public void makeNoise()
{
noise = "Woof!";
}
public String getNoise()
{
return noise;
}
public Food eat(Food x) throws Exception
{
if (x instanceof Meat) {
return x;
} else {
throw new Exception("Carnivores only eat meat!");
}
}
public void eat(Food food, int count) {
while (count > 0) {
addFood(food);
count--;
}
}
public void addFood(Food inFood)
{
foodGroup.add(inFood);
}
}
演示 class
import java.util.ArrayList;
public class Demo {
public static void main(String[] args)
{
ArrayList<Animal> animalGroup = new ArrayList<>();
//Add new Animals with properties such as name and age?
Animal wolf1 = new Wolf();
addAnimal(new Wolf("lnb1g16", 6));
}
public static void addAnimal(Animal inAnimal)
{
animalGroup.add(inAnimal);
}
}
显然,我想在演示 class 中基于这些先前的 classes 创建一组动物?我不明白这是如何完成的,也不知道为什么我需要创建另一个主要方法。任何关于如何编写演示 class 的帮助将不胜感激,因为我对我被要求做的事情感到困惑,谢谢。
演示class
public static void main(String[] args)
{
ArrayList<Animal> animalGroup = new ArrayList<>();
//Add new Animals with properties such as name and age?
Animal wolf1 = new Wolf();
animalGroup.add(new Wolf("sam", 5));
animalGroup.add(new Wolf("george", 5));
animalGroup.add(new Wolf("patrick", 7));
}