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