9. Conditions that cannot be inherited 1

Source: Internet
Author: User

 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

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.