定义一个测试类AnimalTest,定义一个ArrayList对象成员animalList;在该类的main方法中分别定义3个猫对象和3个马对象,并添加到animalList;通过循环依次打印所有猫对象和所有马对象的信息,并分别调用这些对象的walk方法和wakeup方法。
时间: 2023-08-31 08:18:44 浏览: 138
java代码-定义一个员工信息类Employee,使用ArrayList对员工信息进行添加和显示
### 回答1:
以下是代码实现:
```java
import java.util.ArrayList;
public class AnimalTest {
private ArrayList<Animal> animalList;
public AnimalTest() {
animalList = new ArrayList<>();
}
public static void main(String[] args) {
AnimalTest animalTest = new AnimalTest();
// 定义3个猫对象
Cat cat1 = new Cat("Tom", 2, "orange");
Cat cat2 = new Cat("Jerry", 3, "gray");
Cat cat3 = new Cat("Garfield", 5, "orange");
// 定义3个马对象
Horse horse1 = new Horse("Lucy", 6, "brown");
Horse horse2 = new Horse("Charlie", 4, "white");
Horse horse3 = new Horse("Blacky", 8, "black");
// 添加到animalList
animalTest.animalList.add(cat1);
animalTest.animalList.add(cat2);
animalTest.animalList.add(cat3);
animalTest.animalList.add(horse1);
animalTest.animalList.add(horse2);
animalTest.animalList.add(horse3);
// 打印所有猫对象和所有马对象的信息,并分别调用这些对象的walk方法和wakeup方法
for (Animal animal : animalTest.animalList) {
if (animal instanceof Cat) {
System.out.println(((Cat) animal).toString());
((Cat) animal).walk();
((Cat) animal).wakeup();
} else if (animal instanceof Horse) {
System.out.println(((Horse) animal).toString());
((Horse) animal).walk();
((Horse) animal).wakeup();
}
}
}
static class Animal {
private String name;
private int age;
private String color;
public Animal(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
public void walk() {
System.out.println(name + " is walking.");
}
public void wakeup() {
System.out.println(name + " is waking up.");
}
@Override
public String toString() {
return "Animal{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
}
static class Cat extends Animal {
public Cat(String name, int age, String color) {
super(name, age, color);
}
}
static class Horse extends Animal {
public Horse(String name, int age, String color) {
super(name, age, color);
}
}
}
```
### 回答2:
定义一个测试类AnimalTest,其中包含一个ArrayList对象成员animalList。
import java.util.ArrayList;
public class AnimalTest {
private ArrayList<Animal> animalList;
public AnimalTest() {
animalList = new ArrayList<>();
}
public static void main(String[] args) {
AnimalTest animalTest = new AnimalTest();
// 定义3个猫对象,并添加到animalList
Cat cat1 = new Cat("Tom", 2);
animalTest.animalList.add(cat1);
Cat cat2 = new Cat("Mimi", 3);
animalTest.animalList.add(cat2);
Cat cat3 = new Cat("Kitty", 1);
animalTest.animalList.add(cat3);
// 定义3个马对象,并添加到animalList
Horse horse1 = new Horse("Thunder", 5);
animalTest.animalList.add(horse1);
Horse horse2 = new Horse("Lightning", 4);
animalTest.animalList.add(horse2);
Horse horse3 = new Horse("Spirit", 6);
animalTest.animalList.add(horse3);
// 打印所有猫对象的信息并调用walk方法和wakeup方法
for (Animal animal : animalTest.animalList) {
if (animal instanceof Cat) {
Cat cat = (Cat) animal;
System.out.println("猫的信息:");
System.out.println("名字:" + cat.getName());
System.out.println("年龄:" + cat.getAge());
cat.walk();
cat.wakeup();
}
}
// 打印所有马对象的信息并调用walk方法和wakeup方法
for (Animal animal : animalTest.animalList) {
if (animal instanceof Horse) {
Horse horse = (Horse) animal;
System.out.println("马的信息:");
System.out.println("名字:" + horse.getName());
System.out.println("年龄:" + horse.getAge());
horse.walk();
horse.wakeup();
}
}
}
}
定义Animal类作为猫和马的父类,包含名字和年龄属性,以及walk和wakeup方法。
public abstract class Animal {
private String name;
private int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public abstract void walk();
public abstract void wakeup();
}
定义Cat类继承自Animal类,重写walk和wakeup方法。
public class Cat extends Animal {
public Cat(String name, int age) {
super(name, age);
}
@Override
public void walk() {
System.out.println("猫走路");
}
@Override
public void wakeup() {
System.out.println("猫醒来");
}
}
定义Horse类继承自Animal类,重写walk和wakeup方法。
public class Horse extends Animal {
public Horse(String name, int age) {
super(name, age);
}
@Override
public void walk() {
System.out.println("马走路");
}
@Override
public void wakeup() {
System.out.println("马醒来");
}
}
这样就实现了在AnimalTest类的main方法中定义3个猫对象和3个马对象,并添加到animalList;通过循环依次打印所有猫对象和所有马对象的信息,并分别调用这些对象的walk方法和wakeup方法。
### 回答3:
测试类AnimalTest可以定义如下:
```java
import java.util.ArrayList;
public class AnimalTest {
private ArrayList<Animal> animalList;
public AnimalTest() {
animalList = new ArrayList<>();
}
public void addAnimal(Animal animal) {
animalList.add(animal);
}
public void printAllAnimals() {
for (Animal animal : animalList) {
System.out.println(animal.toString());
}
}
public void walkAllAnimals() {
for (Animal animal : animalList) {
animal.walk();
}
}
public void wakeUpAllAnimals() {
for (Animal animal : animalList) {
animal.wakeup();
}
}
public static void main(String[] args) {
AnimalTest animalTest = new AnimalTest();
// 分别定义3个猫对象和3个马对象,并添加到animalList
Cat cat1 = new Cat("Tom", 2);
Cat cat2 = new Cat("Mimi", 3);
Cat cat3 = new Cat("Kitty", 1);
animalTest.addAnimal(cat1);
animalTest.addAnimal(cat2);
animalTest.addAnimal(cat3);
Horse horse1 = new Horse("Black Beauty", 5);
Horse horse2 = new Horse("Spirit", 4);
Horse horse3 = new Horse("Thunder", 6);
animalTest.addAnimal(horse1);
animalTest.addAnimal(horse2);
animalTest.addAnimal(horse3);
// 打印所有猫对象和所有马对象的信息
animalTest.printAllAnimals();
System.out.println();
// 调用对象的walk方法和wakeup方法
animalTest.walkAllAnimals();
System.out.println();
animalTest.wakeUpAllAnimals();
}
}
```
定义了AnimalTest类,其中包含了一个ArrayList对象成员`animalList`,用于存储Animal对象。在该类中提供了方法`addAnimal`用于向`animalList`中添加Animal对象,`printAllAnimals`用于遍历并打印所有的Animal对象的信息,`walkAllAnimals`和`wakeUpAllAnimals`分别用于遍历并调用所有Animal对象的walk方法和wakeup方法。
在main方法中,首先创建了AnimalTest对象`animalTest`。然后分别创建了3个猫对象和3个马对象,并通过`addAnimal`方法将它们添加到`animalList`中。接着调用`printAllAnimals`方法打印出所有动物的信息,然后分别调用`walkAllAnimals`和`wakeUpAllAnimals`方法,输出所有动物的走和醒来信息。
阅读全文