每日代码系列(19)
1 interface Animal { 2 public abstract void cry(); 3 public abstract String getAnimalName(); 4 } 5 class Simulator { 6 public void playSound(Animal an) { 7 an.cry(); 8 an.getAnimalName(); 9 } 10 } 11 class Dog implements Animal { 12 public void cry() { 13 System.out.println("汪汪..."); 14 } 15 public String getAnimalName() { 16 return "Dog"; 17 } 18 } 19 class Cat implements Animal { 20 public void cry() { 21 System.out.println("喵喵..."); 22 } 23 public String getAnimalName() { 24 return "Cat"; 25 } 26 } 27 public class Test6_4 { 28 public static void main(String[] args) { 29 Simulator sim=new Simulator(); 30 sim.playSound(new Dog()); 31 sim.playSound(new Cat()); 32 } 33 }
上午Java考试,这是最后一道大题。这能难倒我?