动物期末代码
Animal.java public abstract class Animal { public abstract void cry(); public abstract String getAnimalName(); } Simulator.java public class Simulator { public void playSound(Animal animal) { System.out.print("现在播放"+animal.getAnimalName()+"类的声音:"); animal.cry(); } } Dog.java public class Dog extends Animal { public void cry() { System.out.println("汪汪...汪汪"); } public String getAnimalName() { return "狗"; } } Cat.java public class Cat extends Animal { public void cry() { System.out.println("喵喵...喵喵"); } public String getAnimalName() { return "猫"; } } Application.java public class Example5_13 { public static void main(String args[]) { Simulator simulator = new Simulator(); simulator.playSound(new Dog()); simulator.playSound(new Cat()); } }