第五章作业
程序员文章站
2024-03-01 19:07:16
...
public class Application{
public static void main(String[] args){
Simulator simulator = new Simulator();
simulator.playSound(new Dog());
simulator.playSound(new Cat());
}
}
class Simulator{
void playSound(Animal animal){
animal.cry();
System.out.println("动物种类:"+animal.getAnimalName());
}
}
abstract class Animal{
abstract void cry();
abstract String getAnimalName();
}
class Dog extends Animal{
void cry(){
System.out.println("汪汪.....");
}
String getAnimalName(){
return "狗";
}
}
class Cat extends Animal{
void cry(){
System.out.println("喵喵.....");
}
String getAnimalName(){
return "猫";
}
}