设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声 。
package bzu.animal;
public abstract class Animal {
abstract void cry();
abstract String getAnimalName();
}
package bzu.animal;
public class Dog extends Animal{
void cry() {
System.out.println("汪汪...汪汪");
}
String getAnimalName() {
return "狗狗";
}
}
package bzu.animal;
public class Cat extends Animal {
void cry() {
System.out.println("喵喵....喵喵");
}
String getAnimalName() {
return "猫咪";
}
}
package bzu.animal;
public class Simulator {
public void playSound(Animal animal){
System.out.println("现在播放"+animal.getAnimalName()+"的声音:");
animal.cry();
}
}
package bzu.animal;
public class Test {
public static void main(String[] args) {
Simulator simulator=new Simulator();
Dog dog=new Dog();
Cat cat=new Cat();
simulator.playSound(dog);
simulator.playSound(cat);
}
}
![这里写图片描述](https://img-blog.csdn.net/20171024223414964?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRXZhbjE5ODcwNTA0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)