(2) Java Foundation inheritance

Source: Internet
Author: User

Defining Classes Humanhas several properties and functions, and defines its subclass man,Woman;

in the main class in test, create subclasses, parent classes, and up-to-transform objects, and test their attributes.

Human:

Package eryi; Public classHuman {PrivateString name; Private intAge ;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     Public voidspeak () {System. out. println ("I'm a kid ."); }}

Mans:

Package eryi;  Public class Mans extends Human {    publicvoid  speak ()    {        System. out. println (" I am a boy ");}    }

Woman:

Package eryi;  Public class Woman extends Human {    publicvoid  speak ()    {        System. out. println (" I am a girl ");}    }

Human1:

Package eryi; Public classHuman1 { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsHuman H=NewMan (); H.setage ( -); H.setname ("Name: Zhang San. ");                H.speak (); System. out. println (H.getname () +"Age:"+h.getage ()); Human H1=NewWoman (); H1.setage ( -); H1.setname ("name: quiet. ");                H1.speak (); System. out. println (H1.getname () +"Age:"+h1.getage ()); }}

Write a Animal class, having attributes: kind; With function: Eat, sleep. Define its sub-class Fish

and the Dog, which defines the main class E, creates its objects separately in its main method and tests the properties of the object.

Animal:

Package eryi; Public classAnimal {PrivateString Zhonglei; PrivateString name;  PublicString Getzhonglei () {returnZhonglei; }     Public voidSetzhonglei (String zhonglei) { This. Zhonglei =Zhonglei; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }         Public voideat () {} Public voidsleep () {}}

Dog:

Package eryi;  Public class Dog extends animal{    publicvoid  eat ()    {        System. out. println (" I like to eat meat ")    ;    }  Public void sleep ()    {        System. out. println (" I like to sleep in ");}    }

Fish:

Package eryi;  Public class Fish extends animal{    publicvoid  eat ()    {        System. out. println (" I like to eat fish food ")    ;    }  Public void sleep ()    {        System. out. println (" I like to get up early ");}    }

Test:

Package eryi; Public classAnimal1 { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsDog D=NewDog (); D.setname ("JoJo"); D.setzhonglei ("Bo Mei"); System. out. println ("I am a"+d.getzhonglei () +". My name is"+d.getname ());        D.eat ();                D.sleep (); Fish F=NewFish (); F.setname ("Nemo"); F.setzhonglei ("Clown Fish"); System. out. println ("I am a"+f.getzhonglei () +". My name is"+f.getname ());        F.eat ();            F.sleep (); }}

(2) Java Foundation inheritance

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.