怎么设计一个动物声音“模拟器”?
发布网友
发布时间:2022-04-30 16:26
我来回答
共1个回答
热心网友
时间:2022-06-27 10:40
public class Test {
public static void main(String[] args) {
Simulator simulator = new Simulator();
Animal cat = new Cat();
Animal dog = new Dog();
simulator.playSound(cat);
simulator.playSound(dog);
}
}
abstract class Animal{
public String animalName;
public abstract void cry();
public abstract String getAnimalName();
}
class Simulator{
public void playSound(Animal animal){
System.out.println(animal.getAnimalName());
animal.cry();
}
}
class Cat extends Animal{
public Cat(){
this.animalName = "加菲猫";
}
public void cry() {
System.out.println("Cat cry!");
}
public String getAnimalName() {
return this.animalName;
}
}
class Dog extends Animal{
public Dog(){
this.animalName = "欧迪";
}
public void cry() {
System.out.println("Dog cry!");
}
public String getAnimalName() {
return this.animalName;
}
}