II. Inner Classes
1. Internal hidden basic usage
1) If an Inner Class object is generated outside of the peripheral Class's Non-Static function, you can specify the type of the object in the form of OuterClassName.innerClassName. And in the Non-Static function is not available.
PUBLIC CLASS ExplicitStatic {
Class contents {
Private INT i = 11;
Public int value () {Return I;}
}
Class destination {
PRIVATE STRING LABEL;
Destination (String WHERETO) {
Label = WHERETO;
}
String readlabel () {Return Label;
}
Public Destination to (String s) {
/ / Inner Class object directly in the Non-Static function of Outer Class
Return New Destination (s); // (1)
}
Public contents last () {
Return new contents (); // (1)
}
Public Void Ship (String Dest) {
/ / Can pass innerclassname directly in the Non-Static function of Outer Class
// to specify the object type
CONTENTS C = CONT ();
Destination D = To (DEST);
System.out.println (D.Readlabel ());
}
Public static void main (String [] args) {
ExplicitStatic P = New ExplicitStatic ();
P.SHIP ("Tanzania");
ExplicitStatic Q = New ExplicitStatic ();
/ / Generate Inner Class object within the non-Non-Static function of Outer Class
ExplicitStatic.Contents C = q.cont ();
ExplicitStatic.Destination D = Q.to ("Borneo");
/ / Do not generate Inner Class objects directly in the Static function
// new contents ();
}
}
2) For the Non-Static Inner Class, the Non-Static function of the peripheral Class can generate an Inner Class object with the NEW, as above (1). But to create an Inner Class object in a non-Non-Static function, you must be associated with an object of its Enclosing Class.
3) The upward transformation of Inner Class
When an Inner Class object is turned upward into interface, we get just a reference.
Interface destination {
String readlabel ();
}
Interface contents {
Int value ();
}
Class pacel3 {
Private class pcontents imports contents {
Private INT i = 11;
Public int value () {Return I;}
}
Protected class pdestination usments destination {
Private string label; pdestination (string whereto) {
Label = WHERETO;
}
Public string readlabel () {return Label;}
}
Public Destination to (String s) {
Return new pdestination (s);
}
Public contents last () {
Return new PContents ();
}
}
PUBLIC CLASS ExplicitStatic {
Public static void main (String [] args) {
PARCEL3 P = New Parcel3 ();
// Transform the Inner Class object upward
CONTENTS C = P.cont ();
Destination D = P.to ("Borneo");
}
}
Although we can't call PContents Class in the ExplicitStatic Class, we can call a PContents Class object to Contents to CONTENTS.
4) The role of Inner Class is within the scope of the Inner Class. But Inner Class can be inherited outside of its scope (see 4).
Interface contents {
Int value ();
}
Class pacel3 {
// PContents1 Class's scope of Parcel3 Class
Private class pcontents1 imports contents {
Private INT i = 11;
Public int value () {Return I;}
}
Public contents cont1 () {
Return new pContents1 ();
}
Public contents cont2 () {
// PCONTENTS2 CLASS scope is within the function Cont2
Class PContents2 Implements Contents {
Private INT i = 11;
Public int value () {Return I;}
}
Return new PContents2 ();
}
/ / Do not use PContents2 Class outside the function Cont5
/ *
Public Contents Cont22 () {
Return new PContents2 ();
}
* /
Public Contents Cont3 (Boolean B) {
IF (b) {
// PCONTENTS3 CLASS scope is the current IF
Class PContents3 Implements Contents {
Private INT i = 11;
Public int value () {Return I;}
}
Return new PContents3 ();
}
/ / Can't use PContents3 Class outside IF
// Return New PContents3 ();
Return NULL;
}
}
PUBLIC CLASS ExplicitStatic {
Public static void main (String [] args) {
PARCEL3 P = New Parcel3 ();
Contents c1 = p.cont1 ();
Contents C2 = P.cont2 ();
Contents C3 = P.cont3;
}
}
2. Connection relationship between intimacity and peripheral Enclosing Class
2.1 Non-Static Inner Class
1) Inner Class can access all members of Enclosing Class (including Private members), just like the Inner Class owned to these members. That is, INNER CLASS is natural with access to all members of Enclosing Class. 2) When the Inner Class object is generated, it is necessary to associate an object to its Enclosing Class (this Enclosing Class object is the manufacturer of the Inner Class object). While constructing Inner Class objects, there is a Reference that has its enclosing class object.
Cause: Because INNER CLASS can access all members of Enclosing Class, then when an Inner Class is generated, the compiler will automatically add a Reference to the Enclosing Class object to the Inner Class object (this Reference is hidden). So INNER CLASS is generated, must be associated with an object of its Enclosing Class.
3) The INCLOSING Class object is available in the INCLOSING CLASS object, which is the same enclosing class object.
Interface destination {
String readlabel ();
}
Interface contents {
Int value ();
}
Class pacel3 {
INT I1 = 10;
Private string s1 = "pacel3_";
Parcel3 (string s) {
S1 = S;
}
Private class pcontents imports contents {
// Members of Enclosing Class (1)
Private Int i2 = I1;
Private string s2 = S1;
PCONTENTS (int Num) {
System.out.println (" Num ": i2 = " i2 ", S2 = " S2);
}
Public int value () {return 1;}
}
Public Contents Cont (INT I) {
Return New PCONTENTS (I);
}
}
PUBLIC CLASS ExplicitStatic {
Public static void main (String [] args) {
PARCEL3 P1 = New Parcel3 ("1");
Contents c1 = p1.cont (1);
Contents C2 = p1.cont (2);
PARCEL3 P2 = New Parcel3 ("2");
C2 = p2.cont (3);
C2 = p1.cont (4);
}
}
The result is:
1: i2 = 10, S2 = PARCEL3_1
2: i2 = 10, S2 = PARCEL3_1
3: I2 = 10, S2 = PARCEL3_2
4: i2 = 10, S2 = parcel3_1
In (1) called members of Enclosing Class in Inner Class. The results show that the INCLOSING CLASS object P1 generated inner Class object call is a member in the same Enclosing Class object, such as 1, 2, 4.2.2 Static Inner Classes in the result (static inside)
1) When the Static Inner Classes object is generated, there is no need to exist at the time of an enclosing class object.
2) You can only access static members in Enclosing Class in the Static Inner Classes object.
Interface contents {
Int value ();
}
Class pacel1 {
Private static string s1 = "pacel3_";
Private string S11 = "pacel3_";
Parcel1 (string s) {
S1 = S;
}
Protected static class pcontents usments contents {
// You can only access S1 in Enclosing Class
String S2 = S1;
// S11 is not a STATIC member, not accessible
// String 22 = S11;
PCONTENTS (int Num) {
System.out.Println (" Num ": S2 = " S2);
}
Public int value () {return 1;}
}
Public Static Contents Cont (INT i) {
Return New PCONTENTS (I);
}
}
PUBLIC CLASS ExplicitStatic {
Public static void main (String [] args) {
PARCEL1 P1 = New Parcel1 ("1");
Contents c1 = p1.cont (1);
C1 = pacel1.cont (2); // (1)
PARCEL1 P2 = New Parcel1 ("2");
C1 = p2.cont (3);
C1 = PARCEL1.CONT (4); // (1)
}
}
Because the internal hidden PCONTENTS CLASS is static, it is directly generated by the Enclosing Class object through the Enclosing Class object in (1).
2.3 None of the INNER CLASS is nestled, and all Outer Class members can
Be accessed by it.
Class MNA {
PRIVATE VOID F () {}
Class a {
Private void g () {}
Class b {
Void h () {
g ();
f ();
}
}
}
}