In-depth understanding of nested and internal classes, what is nesting and internal classes? Another class can be defined in one class, which is called nested classes, which has two types: static nested classes and non-static nested classes. The static nested class is very small, and most importantly, non-static nested classes, that is, it is called an internal class (Inner). Nested classes are introduced from JDK1.1. The INNER class can be divided into three: one, inside class directly defined in a class (external class); two, in the internal class defined in a method (external class); three, anonymous interior class. Below, I will explain the use and precautions of these nested classes. Second, static nested classes as shown below are defined a static nested class,
public class StaticTest {private static String name = "javaJohn"; private String id = "X001"; static class Person {private String address = "swjtu, chenDu, China"; public String mail = "josserchai@yahoo.com"; / / Internal class public member public void display () {//system.out.println (ID); // cannot directly access the unstatic member system.out.println (name) of the external class; // can only access the external class directly Static member System.out.Println ("Inner" address; // Access the internal class member. }} public void printinfo () {Person Person = new person (); person.display (); // system.out.println (mail); // is not accessible //system.out.println (Address );/// You cannot access system.out.println (Person.address); // You can access the private member of the internal class system.out.println (Person.mail); // You can access the internal class public member} public static void main (String " ] args) {statictest statictest = new staticTest (); statictest.printinfo ();}}
Inside the static nested class, non-static members of the external classes cannot be accessed, which is limited by the "static method without direct access to nonstatic members" in the Java syntax. If you want to access the variables of the external class, you must solve it through other methods. For this reason, the static nested class is very small. Note that some members of the external class access to the internal classes are somewhat special and cannot be directly accessed, but they can be accessed through internal classes because all members and methods in static nested are still static. At the same time, the internal static class Person is only visible in the class StaticTest, and if references or initialization in other classes, it is wrong. Third, define internal classes as shown below to define two internal classes in the external class and their call relationships:
Public class {int out = 10; Class Inner {public int y = 10; private int z = 9; int m = 5; public void display () {system.out.println ("Display Outer_x:" Outer_x); } private void display2 () {system.out.println ("Display Outer_X:" Outer_x);}}} void test () {inner inner = new inner (); inner.display (); inner.display2 (); / /System.out.println ("Inner Y:" Y); // You cannot access the internal variable system.Println ("Inner Y: inner.y); // You can access System.out.Println ( "Inner Z:" inner.z); // You can access System.out.Println ("Inner M: Inner.m); // Access INNERTWO INNERTWO = New Innertwo (); inNerTwo.show (); } Class innertwo {INNER INNERX = New Inner (); public void show () {// system.out.println (y); // Does not access the INTER Y member //system.out.println (Inner.y); // No member and method INNERX.Display (); // can access innerx.display2 (); // can access System.Out.println (INNERX.PRINTLN (INNERX.PRINTLN). Println (innerx.z); // can access system.out.println (Innerx.m); // can access}}} public static void main (String args []) {Outer Outer = new outer (); Outer.test ();}}} The above code needs to be explained, for internal classes, usually do not add PUBLIC or PRIVATE such as the Class keyword defined class, if not add Any influence, it seems that these qualifiers have no effect on the variables and methods of the internal classes (?). In addition, it is to be noted that internal class INNER and INNTERTWO are only known in the scope of class Outer. If any code outside the class OUTER is trying to initialize the class Inner or use it, compile will not pass. At the same time, the internal class variable members can only be visible inside, if the external class or the inner class of the same level requires access, the method in the sample program is required, and the internal class variable cannot be directly accessed. 4. Define internal classes as shown below in the method to define an internal class within the method:
Public class funouter {int out_x = 100; public void test () {class inner {string x = "x"; void display () {system.out.println (out_x);}} inner inner = new inner (); inner .display ();} public void showstr (String str) {// public string str1 = "test inner"; // is not definable, only final modification // static string str4 = "static str"; // is not definable, Allow Final String str2 = "test inner"; final string str3 = "final str"; class innertwo {public void test () {system.out.println (out_x); // can directly access the external class variable // System.out.println (STR); // Do not access non-final variables inside this method //system.out.println (STR2) (Str2) (STR2) ; // only access to the final variable member of this method}} inNerTwo innertwo = new inNNERTWO (); inNerTwo.testPrint ();} public void use () {// inner innerobj = new inner (); // At this time INNER is not visible. //System.out.println (Inner) at this time is not visible. } public static void main (string [] args) {funouter outr = new funouter ();}} From the above routine we can see that the visibility of the internal class defined within the method is smaller. It is only visible inside the method, which is not visible in the external class (and other methods of external classes). At the same time, it has a feature that is not accessible for member variables within the method within the method. It can only access the Final member of this method. At the same time, another payment is that the internal definition member is only allowed to modify or do not repair the decorator, and other statics are not available. 5. Anonymous internal classes are shown below to define an anonymous internal class: anonymous internal classes are usually used on Java event processing.
import java.applet *;. import java.awt.event *;. "! Mouse Pressed" public class AnonymousInnerClassDemo extends Applet {public void init () {addMouseListener (new MouseAdapter () {public void mousePressed (MouseEvent me) {showStatus ( );}})} Public void showstatus (string str) {system.out.println (STR);}}
In the above example, the method AddMouseListener accepts an object-type parameter expression, so in the parameter, we define an anonymous internal class this class is a mouseadapter type class, which defines one inheritance in this class. MousePressed, the entire class is made as a parameter. This class has no name, but it is automatically instantiated when this expression is executed. At the same time, this anonymous internal class is defined inside the AnonymousinnerClassDemo class, so it can access its method showstatus. This is consistent with the previous internal classes. Sixth, other problems used by internal class pass, we can clearly see some of the use methods of internal classes, while in many cases, internal classes are used in event processing such as Java, or as a value object. At the same time, we need to pay attention to the last problem, that is, the internal class is as defined as other classes, and it can also inherit the external other packages and interfaces that implement other places outside. Also it can also inherit the other internal classes of the same level, and even inherit the external class itself. Below we give the last example as the end:
Public class layer {// Layer member Variables private string teststr = "teststr"; // Person class, base class class person {string name; email email; public void setname (String name = namestr;}) {this.name = namestr;} public String getName () {return this.name;} public void setEmail (Email emailObj) {this.email = emailObj;} public String getEmail () {return this.email.getMailStr ();} // inner class inner class , a multilayer inner class class Email {String mailID; String mailNetAddress; Email (String mailId, String mailNetAddress) {this.mailID = mailId; this.mailNetAddress = mailNetAddress;} String getMailStr () {return this.mailID "@" This.mailnetAddress;}}} // Another internal class inherits the external class itself class childlayer extends layer {void print () {system.out.println (super.teststr); // Access the member variable of the parent class}} // Another internal class inherits internal classes Personclass OfficePerson Extends Person {void show () {system.out.println (name); system.out.println (getemail ());}}} // External class test method PUBLIC VOID TESTFUNCTION ) {// Test the first internal class ChildLayer childlayer = new childlayer (); childlayer.print (); // Test the second internal class OfficePerson OfficePerson = new officePerson (); officePerson.setname ("abner chai"); "ABNER CHAI"); // Note that this object .New comes with the sub-objects of the object // instead of Person.new email (...) // nor new person.email (...) OfficePerson.New Email (OfficePerson.New Email "josserchai", "yahoo.com")); officePerson.show ();} public static void main (string [] args) {layer layer = new layer (); layer.testfunction ();}} Copyright by Abnerchai, 2004