Java Programming Ideology (2nd) Learning Notes (8) -3

zhaozj2021-02-17  47

3. How to generate an Inner Class object

3.1 Non-Static Internal Inclusion

1) Can be generated directly through New in the Non-Static function of Enclosing Class

2) In the static function or other Class of Enclosing Class, there must be an Enclosing Class object at the same time (whose reason is described above 2.1).

Interface contents {

Int value ();

}

Class pacel1 {

Protected class pcontents imports contents {

Public int value () {return 1;}

}

Public contents last () {

// Generate a PContents Class object directly in the Non-Static function

Return new PContents ();

}

Public static void test (string [] args) {

PARCEL1 P1 = New paracel1 ();

// Generate through the external class PARCEL1 object in the Static function

Contents c1 = p1.cont (); // call function

C1 = p1.new pContents (); // through New

}

}

PUBLIC CLASS ExplicitStatic {

Public static void main (String [] args) {

// Produce by external PARCEL1 object

PARCEL1 P1 = New paracel1 ();

Contents c1 = p1.cont (); // call function

C1 = p1.new pContents (); // through New

}

}

3.2 STATIC Inclusion

1) In addition to the method of generating Non-Static hidden objects, it may be generated by existing an Enclosing Class object.

Interface contents {

Int value ();

}

Class pacel1 {

Protected static class pcontents usments contents {

Public int value () {return 1;}

}

Public contents last () {

// Generate a PContents Class object directly in the Non-Static function

Return new PContents ();

}

Public static contents cont1 () {

// Generate a PContents Class object directly in the Static function

Return New PContents (); // (1)

}

Public static void test (string [] args) {

PARCEL1 P1 = New paracel1 ();

// Generate through the external class PARCEL1 object in the Static function

Contents c1 = p1.cont (); // call function

C1 = p1.new pContents (); // through New

// Generate a PContents Class object directly in the Static function

C1 = new PContents (); // (1)

}

}

PUBLIC CLASS ExplicitStatic {

Public static void main (String [] args) {

// Produce by external PARCEL1 object

PARCEL1 P1 = New paracel1 ();

Contents c1 = p1.cont (); // call function

C1 = p1.new pContents (); // Directly generated through new //

C1 = pacel1.cont1 (); // (2)

}

}

The code in (1) and 9 (2) above can only pass when the PContents Class is STITIC. (1) See 2.1.

2. Inner Class inheritance

1) Inner Class can be inherited. The DRFAULT constructor of the Inner Class's Drived Class must be incompatible to the Outer Object and call the build function of the Outer Class in the constructor.

Class withinner {

Class inner {}

}

Class inheritinner extends withinner.inner

{

// inheritinner () {} compilation error

Inheritinner (WITHINNER WI) {wi.super ();

}

PUBLIC CLASS ExplicitStatic {

Public static void main (String [] args) {

WITHINNER WI = New WITHINNER ();

Inheritinner II = New Inheritinner (Wi);

}

}

2) Overriding Inner Class does not have polymorphism.

Class egg {

Class Yolk {

Public yolk () {

System.out.println ("Egg.Yolk ()");

}

}

Private yolk y;

Public Egg () {

System.out.Println ("New Egg ()");

y = new yolk (); // (1)

}

}

Class Bigegg Extends Egg {

// (2) Try overwriting Inner Class

Class Yolk {

Public yolk () {

System.out.println ("BiGegg.Yolk ()");

}

}

}

PUBLIC CLASS ExplicitStatic {

Public static void main (String [] args) {

New bigegg (); // (3)

}

}

The result is:

New Egg ()

Egg.yolk ()

In (2) we tried to overwrite the Inner Class. When a BiGeGG is generated by (3), the constructor of the EGG will be called. The Egg.Yolk Class object is generated at the (1) of the EGG constructor, not a subclass BIGEGG.YOLK CLASS object.

**: As shown above, the two INNER CLASs described above are completely independent individuals, each with its own namespace.

转载请注明原文地址:https://www.9cbs.com/read-29201.html

New Post(0)