8.6 Practical Exercises

Source: Internet
Author: User

The second question:

Bird Class Code:

1  PackageZuoye;2 3  Public classBird {4     PrivateString name;5     PrivateString Yanse;6      PublicString GetName () {7         returnname;8     }9      Public voidsetName (String name) {Ten          This. Name =name; One     } A      PublicString Getyanse () { -         returnYanse; -     } the      Public voidsetyanse (String yanse) { -          This. Yanse =Yanse; -     } -      PublicBird (string name, String yanse) { +      -System.out.println ("Parent class constructor method"); +          This. Name =name; A          This. Yanse =Yanse; at          -     } -      Public voidEat () -     { -System.out.println ("I Can Eat worms"); -     } in      -      Public voidFly () to     { +System.out.println ("I Can Fly"); -     } the  *}
View Code

Yingwu Class Code:

1  PackageZuoye;2 3  Public classYingwuextendsBird {4 5      PublicYingwu () {6         Super("Parrot", "green");7System.out.println ("subclass constructor Method"));8     }9     Ten      Public voidSing () One     { ASystem.out.println ("I Love Singing"); -     } -      Public voidFly () the     { -System.out.println ("I like to fly while singing: The Boundless Horizon is my Love ~"); -     } -      +  -}
View Code

Test Class Code:

1  PackageZuoye;2 3  Public classTestbird {4 5      Public Static voidMain (string[] args) {6         //TODO auto-generated Method Stub7Bird b1=NewBird ("Sparrow", "grey");8System.out.println ("I Am" +b1.getname () + "color is" +b1.getyanse ());9 b1.eat ();Ten b1.fly (); One System.out.println (); AYingwu y1=NewYingwu (); -System.out.println ("I Am" +y1.getname () + "color is" +y1.getyanse ()); - y1.eat (); the y1.sing (); - y1.fly (); - System.out.println (); -Bird b2=NewYingwu (); +System.out.println ("I Am" +b2.getname () + "color is" +b2.getyanse ()); - b2.eat (); + b2.fly (); A          at          -     } -  -}
View Code

Question three:

The code goes like this, when you construct a subclass object, you call the constructor of the subclass, and if you need to add parameters to the constructor, you can continue to call the subclass or other methods of the parent class.

8.6 Practical Exercises

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.