package cn.edu.whpu;
abstract class Animal{
abstract void cry();
abstract String getAnimalName();
}
class Simulator{
void playSound(Animal animal) {
System.out.print("现在播放"+animal.getAnimalName()+"的声音:");
animal.cry();
}
}
class Dog extends Animal{
void cry() {
System.out.println("汪汪汪");
}
String getAnimalName() {
return "Dog";
}
}
class Cat extends Animal{
void cry() {
System.out.println("喵喵喵");
}
String getAnimalName() {
return "Cat";
}
}
public class study {
public static void main(String[] args) {
Simulator simulator=new Simulator();
simulator.playSound(new Dog());
simulator.playSound(new Cat());
}
}
Java课后第五章习题---继承
最新推荐文章于 2023-11-04 10:14:36 发布