Java Animal Sound Simulator

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.