PackageCn.jbit.epet.purview;/*** Doggy class, pet subclass. */ Public classDogextendsPet {PrivateString strain;//Variety /*** A method for constructing a parameter. * @paramName Nickname *@paramStrain Varieties*/ PublicDog (string name, string strain) {Super(name);//This.name=name cannot be used here; This. Strain =strain; } Public voidSetstrain (String strain) { This. Strain =strain; } PublicString Getstrain () {returnstrain; } Public voidprint () {Super. Print (); System.out.println ("I am a" + This. Getstrain () + "dog. "); } /*** Test can not be inherited situation*/ Public voidTest () {//System.out.println (name);//cannot inherit private memberSYSTEM.OUT.PRINTLN (color);//under the same package, you can inherit protected membersSystem.out.println (Avoirdupois);//under the same package, subclasses can inherit the members of the default access permissionSYSTEM.OUT.PRINTLN (ID);//under the same package, you can inherit public members }}
PackageCn.jbit.epet.purview;/*** Pet class, Dog and Penguin parent class. */ Public classPet {PrivateString name = "Anonymous";//Nickname Private intHealth = 100;//Health Value Private intLove = 0;//Degree of intimacy /*The following properties are test access permissions and inheritance conditions*/ intavoirdupois=2;//Weight protectedString color;//Color Public intid=1001;//numbering /*** Non-parametric construction method. */ PublicPet () { This. Health = 95; System.out.println ("The non-parametric construction method of the pet is performed. "); } /*** A method for constructing a parameter. * @paramName Nickname*/ PublicPet (String name) { This. Name =name; } PublicString GetName () {returnname; } Public intGethealth () {returnHealth ; } Public intGetlove () {returnLove ; } /*** Output pet information. */ Public voidprint () {System.out.println ("The Confession of the pet: \ n My name is" + This. Name + ", my health value is" + This. Health+ ", my intimacy with the master is" + This. Love + ". "); }}
Public class Test { /** @param args */public staticvoid main (string[] args) { System.out.println (new Pet (). color); // under the same package, you can use the protected member System.out.println (new Pet (). ID); // under the same package, you can use the public member }}
9. Conditions that cannot be inherited 1