Classroom Exercises: Teaching materials 143 page 4. Programming Questions
Animal class
1 Public Abstract class Animal { 2 3public abstractvoid cry (); 4 Public Abstract String getanimalname (); 5 6 }
Simulator class
1 Public class Simulator { 2 3public void playSound (Animal Animal) { 4 System.out.print (animal.getanimalname () + ":"); 5 animal.cry (); 6 } 7 }
Dog class
Public class extends animal{ publicvoid Cry () { System.out.println ("Wang Woo ..."); public String getanimalname () { return "Puppy"; } }
Cat class
Public class extends animal{ publicvoid Cry () { System.out.println ("Meow meow ..."); public String getanimalname () { return "Kitten"; }
Application class
Public class Application { publicstaticvoid main (string[] args) { Simulator Simulator=new Simulator (); Simulator.playsound (new Dog ()); Simulator.playsound (new Cat ()); } }
Operation Result:
Java Animal Sound simulator