Questions about accessing non-static internal classes in static methods

xiaoxiao2021-04-08  382

Public class outer {

Public string name = "outer";

Public static void main (String Argv []) {

// inner myinner = new inner (); // Directly create this sentence to create a compilation error

Outer myouter = new outer (); // Create an external class object

Outer.inner myinner = myouter.new inner ();

Myinner.showname ();

} // end of main

// The following code is used to test this N annoyance

Public void amethod () {

Outer myouter = new outer ();

Outer.inner myinner = myouter.new inner ();

Myinner.showname ();

}

// Non-static method access non-static internal class

PRIVATE CLASS INNER {

String name = new string ("inner");

Void showname () {

System.out.println (Name);

}

} // End of Inner Class

}

In the non-static method, the non-static internal class is created directly to the internal class: new inner (). Showname (); of course, this N annoying method is also possible to modify the Private Class Inner to change to Static Private Class Inner, then static The access static internal classes are also objects that directly create the internal class, inner myinner = new inner (), or Outer.inner Myinner = new Outer.inner () can also be passed, and this N-annoying method is above. It can be used in three cases.

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

New Post(0)