![](https://img-blog.csdn.net/20171116180512028?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTDEyNzI2OTczMTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![]()
public abstract class Animal {
public abstract void cry();
public abstract String getAnimalName();
}
public class Simulator {
void PlaySound(Animal animal) {
animal.cry();
System.out.println(animal.getAnimalName());
}
}
public class Dog extends Animal {
public void cry() {
System.out.println("汪汪汪!");
}
public String getAnimalName() {
return "dog";
}
}
public class Cat extends Animal {
public void cry() {
System.out.println("喵喵喵!");
}
public String getAnimalName() {
return "cat";
}
}
public class Application {
public static void main(String[] args) {
Simulator simulator=new Simulator();
simulator.PlaySound(new Dog());
simulator.PlaySound(new Cat());
}
}