每日代码系列(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考试,这是最后一道大题。这能难倒我?

Original: https://www.cnblogs.com/ljydbk/p/14132791.html
Author: 李家宇
Title: 每日代码系列(19)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/584527/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球