Java Inheritance--4

Source: Internet
Author: User

Write a Java application that consists of 3 classes: The Monkey class, the People class, and the main class

E. Requirements:

(1) There is a construction method in the Monkey class: Monkey (String s), and there is a public void speak ()

method to output the message "babbling ..." in the Speak method.

(2) The People class is a subclass of the Monkey class, overriding the method speak () in the People class, in the Speak method

In the output "sample, good!" It's going to talk! "Information.

(3) Add method void Think () in the People class and output "Don't talk!" in the Think method. Think hard! ”

The information.

(4) In main class E, the main method of creating monkey and people class object class test the work of these 2 classes

Yes.

Package Text1;public class Monkey {///(1) Monkey class has a constructor method: Monkey (String s), and a public void speak ()//method, output in Speak method "babbling ... "information. Monkey (String s) {System.out.println (s);} public void Speak () {System.out.println ("babbling ...");}}

Package Text1;public class People1 extends Monkey {People1 (String s) {super (s);//TODO auto-generated constructor stub}//(2) The People class is a subclass of the Monkey class, overriding the method speak () in the People class, and outputting the "sample" in the Speak Method//. It's going to talk! "Information. (3) Add method void Think () in the People class and output "Don't talk!" in the Think method. Think hard! "//information. public void  speak () {System.out.println ("The sample is good! It's going to talk! ");} void Think () {System.out.println ("Don't talk!) ");}}

Package Text1;public class E {public static void main (string[] args) {//TODO auto-generated method stub//(4) in main class E's Main method to create monkey and people class The object class tests the work//energy of these 2 classes. Monkey m=new Monkey ("monkey");//Test Monkey Class M.speak (); People1 p=new People1 ("human");//Test People1 Class P.speak ();p. Think ();}}

Java Inheritance--4

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.