题目:
设计一个动物模拟器,模仿动物的叫声。
Animal为接口类,里面有两个抽象方法cry()和getAnimalName()
模拟器类Simulator里面有一个palySound(Animal animal)方法,参数animal重写Animal类中的cry()和getAnimalName();
Dog类和Cat类用于实现接口
主类Aplication运行程序
- package 动物模拟器2;
- public interface Animal {
- public abstract void cry() ;
- public abstract String getAnimalName();
- }
- package 动物模拟器2;
- public class Dog implements Animal {
- public String getAnimalName() {
- System.out.println("大黄狗");
- return "大黄狗";
- }
- public void cry() {
- System.out.println("汪汪汪汪!!!");
- }
- }
- package 动物模拟器2;
- public class Cat implements Animal {
- public String getAnimalName() {
- System.out.println("小花猫");
- return "小花猫";
- }
- public void cry() {
- System.out.println("喵喵喵喵!!!");
- }
- }
- package 动物模拟器2;
- public class Simulator {
- void playsound(Animal animal) {
- animal.getAnimalName();
- animal.cry();
- }
- }
- package 动物模拟器2;
- public class Application {
- public static void main(String args[]) {
- Simulator simulator=new Simulator();
- simulator.playsound(new Dog());
- simulator.playsound(new Cat());
- }
- }