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