要求:1.编写抽象类Animal
2.编写模拟器类Simulator
3.编写Animal类的子类:Dog和Cat类
4.编写主类Application(用户程序)
包含代码:Simulator simulator=new Simulator();
simulator.playSound(new Cat());
simulator.playSound(new Dog());
代码如下:
public abstract class Animal {
abstract void cry();
abstract void getAnimaName();
}
abstract void cry();
abstract void getAnimaName();
}
public class Dog extends Animal{
void cry(){
System.out.println("狗的叫声:汪汪汪");
}
void getAnimaName(){
System.out.println("名字:狗");
}
}
void cry(){
System.out.println("狗的叫声:汪汪汪");
}
void getAnimaName(){
System.out.println("名字:狗");
}
}
public class Cat extends Animal{
void cry(){
System.out.println("猫的叫声:喵喵喵");
}
void getAnimaName(){
System.out.println("名字:猫");
}
}
void cry(){
System.out.println("猫的叫声:喵喵喵");
}
void getAnimaName(){
System.out.println("名字:猫");
}
}
public class Simulator {
void playSound(Animal animal){
animal.getAnimaName();
animal.cry();
}
}
void playSound(Animal animal){
animal.getAnimaName();
animal.cry();
}
}
public class Application {
public static void main(String args[]){
Simulator simulator=new Simulator();
simulator.playSound(new Cat());
simulator.playSound(new Dog());
}
}
public static void main(String args[]){
Simulator simulator=new Simulator();
simulator.playSound(new Cat());
simulator.playSound(new Dog());
}
}
运行结果:名字:猫
猫的叫声:喵喵喵
名字:狗
狗的叫声:汪汪汪
猫的叫声:喵喵喵
名字:狗
狗的叫声:汪汪汪