设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声实验结论
时间: 2024-06-13 17:06:01 浏览: 171
以下是一个Java实现的动物声音模拟器,可以模拟不同动物的叫声:
```java
public class Animal {
private String animalName;
public Animal(String name){
this.animalName = name;
}
public String getAnimalName(){
return animalName;
}
public void cry(){
System.out.print("动物发出了叫声");
}
}
public class Dog extends Animal{
public Dog(){
super("狗");
}
public void cry(){
System.out.print("汪汪汪");
}
}
public class Cat extends Animal{
public Cat(){
super("猫");
}
public void cry(){
System.out.print("喵喵喵");
}
}
public class Simulator{
public void playSound(Animal animal){
System.out.printf("动物:"+animal.getAnimalName()+",发出了");
animal.cry();
System.out.println();
}
}
public class Application{
public static void main(String args[]){
Simulator simulator = new Simulator();
simulator.playSound(new Dog());
simulator.playSound(new Cat());
}
}
```
阅读全文