The Java internal class object is used by the. this,.new

Source: Internet
Author: User

 Public classInnerclass {classContent {Private inti;  Public intGetvlaue () {returni; }    }    classDescription {PrivateString lable; Description (String Lab) { This. lable =Lab; }         PublicString readlable () {returnlable; }    }     PublicContent getcontentinstance () {return NewContent (); }     PublicDescription getdescriptionintance (String lable) {return NewDescription (lable); }     Public voidShip (String lable) {content content=getcontentinstance (); Description Description=getdescriptionintance (lable);        System.out.println (Description.readlable ());    System.out.println (Description.readlable ()); }    /*** Before you create an external class object, you cannot create an inner class object, because the inner class object is secretly connected to the outer class object. <p> * If you want to create an inner class object through a perimeter class object, the simplest way is to declare a method in the perimeter class that points to its inner class object. Another simpler approach * Java supports the use of a perimeter class object. New syntax to express a reference to a perimeter class object *@paramargs*/     Public Static voidMain (string[] args) {innerclass parcle2=NewInnerclass (); Innerclass.content C= Parcle2.NewContent ();        System.out.println (C.getvlaue ()); //parcle2.ship ("HI"); //innerclass.content C = parcle2.getcontentinstance ();//if you want to access an object of an inner class anywhere outside the non-static method of the outer class, you must pass the outclass.xxinnerclass.description D= Parcle2.NewDescription ("Hello");        System.out.println (D.readlable ()); //innerclass.description d = parcle2.getdescriptionintance ("Hello");    }}
View Code

Use the. this,.new

. This is expressed in the inner class object domain through the external class. This points to a relationship in the inner class that points to the perimeter class object reference. The only way to access the properties and methods of the perimeter class object

The. New expression is the opposite of the. This direction when you want to create an inner class object on a perimeter class scope, it is common practice to create a reference to an inner class in the perimeter class to create an inner class, but there is a quicker way

Direct perimeter class. New can express a perimeter class object reference. It is important to emphasize that it is not possible to create a perimeter class object before owning an external class object, because the inner class object is secretly connected to the object that created his outer class.

Change the inner class example above

 Public classInnerclass {classContent {Private inti;  Public intGetvlaue () {returni; }    }    classDescription {PrivateString lable; Description (String Lab) { This. lable =Lab; }         PublicString readlable () {returnlable; }    }     PublicContent getcontentinstance () {return NewContent (); }     PublicDescription getdescriptionintance (String lable) {return NewDescription (lable); }     Public voidShip (String lable) {content content=getcontentinstance (); Description Description=getdescriptionintance (lable);        System.out.println (Description.readlable ());    System.out.println (Description.readlable ()); }    /*** Before you create an external class object, you cannot create an inner class object, because the inner class object is secretly connected to the outer class object. <p> * If you want to create an inner class object through a perimeter class object, the simplest way is to declare a method in the perimeter class that points to its inner class object. Another simpler approach * Java supports the use of a perimeter class object. New syntax to express a reference to a perimeter class object *@paramargs*/     Public Static voidMain (string[] args) {innerclass parcle2=NewInnerclass (); Innerclass.content C= Parcle2.NewContent ();        System.out.println (C.getvlaue ()); //parcle2.ship ("HI"); //innerclass.content C = parcle2.getcontentinstance ();//if you want to access an object of an inner class anywhere outside the non-static method of the outer class, you must pass the outclass.xxinnerclass.description D= Parcle2.NewDescription ("Hello");        System.out.println (D.readlable ()); //innerclass.description d = parcle2.getdescriptionintance ("Hello");    }}
View Code

The Java internal class object is used by the. this,.new

Related Article

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.