First, what is nested class and internal class?
You can define another class within a class, called a nested class (nested classes), which has two types:
Static nested classes and non-static nested classes. Static nested classes use few, most important non-static nested classes, which are called as
Inner class (inner). Nested classes are introduced from JDK1.1. Among them, inner class can be divided into three kinds:
One, the internal class directly defined in a class (external class);
The inner class defined in a method (the method of an external class);
Its three, anonymous inner class.
Below, I will explain the use of these nested classes and considerations.
Second, Static nesting class
The code shown below is defined as a static nested class.
Copy Code code as follows:
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 publicly owned member
public void display () {
SYSTEM.OUT.PRINTLN (ID);//cannot directly access non-static members of external classes
SYSTEM.OUT.PRINTLN (name);//only direct access to static members of external classes
System.out.println ("Inner" +address)//access to this internal class member.
}
}
public void Printinfo () {
person who = new person ();
Person.display ();
System.out.println (mail);/not accessible
SYSTEM.OUT.PRINTLN (address);/not accessible
SYSTEM.OUT.PRINTLN (person.address)//access to private members of internal classes
SYSTEM.OUT.PRINTLN (Person.mail)//access to public members of internal classes
}
public static void Main (string[] args) {
Statictest statictest = new Statictest ();
Statictest.printinfo ();
}
}
Within a static nested class, non-static members of the outer class cannot be accessed, as defined by "static methods cannot directly access Non-static members" in the Java syntax.
If you want to access the variables of the external class, you must solve them by other means, for this reason, the static nesting classes are rarely used. Note that external classes are accessed within
The members of the class are somewhat special and cannot be accessed directly, but they can be accessed through the inner classes, because all members and methods within the static nesting default to
It's static. Also note that the internal static class person is visible only within the statictest scope of the class, and is incorrect if referenced or initialized in other classes.
Iii. defining internal classes in the external class
The code shown below defines two internal classes and their invocation relationships in the outer class:
Copy Code code as follows:
public class outer{
int outer_x = 100;
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);/cannot access internal variables
System.out.println ("Inner y:" + inner.y);/You can access
System.out.println ("Inner z:" + inner.z)//accessible
System.out.println ("Inner m:" + inner.m);//Can be accessed
Innertwo innertwo = new Innertwo ();
Innertwo.show ();
}
Class innertwo{
Inner Innerx = new Inner ();
public void Show () {
System.out.println (y);/no access to Innter y member
SYSTEM.OUT.PRINTLN (INNER.Y)//no direct access to any members and methods of Inner
Innerx.display ()//can access
Innerx.display2 ()//can access
SYSTEM.OUT.PRINTLN (INNERX.Y)//Can be accessed
SYSTEM.OUT.PRINTLN (INNERX.Z)//Can be accessed
SYSTEM.OUT.PRINTLN (INNERX.M)//Can be accessed
}
}
public static void Main (String args[]) {
Outer Outer = new Outer ();
Outer.test ();
}
}
The above code needs to indicate that, for internal classes, the Class keyword is usually defined without public or private restrictions, if added
Have no effect at the same time as if these qualifiers have no effect on the variables and methods of the inner class (?). In addition, it is to be noted that the internal class inner and
Inntertwo is only knowable within the scope of the class outer, if any code outside the class outer attempts to initialize the class inner or use it, the compilation does not
Will pass. At the same time, the variable members of the inner class are visible only internally, and if the external class or the inner class of the same level needs to be accessed, the sample program should be used
Cannot directly access the variables of the inner class.
Define the inner class in the method
The code shown below defines 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";//Undefined, only final adornments allowed
static String STR4 = "static STR";//Undefined, only final adornments allowed
String str2 = "Test Inner";
Final String STR3 = "Final Str";
Class innertwo{
public void Testprint () {
SYSTEM.OUT.PRINTLN (out_x)//Direct access to external class variables
System.out.println (str); the non-final variable inside this method is not accessible
System.out.println (str2); Non-final variables inside this method are not accessible
System.out.println (STR3);//Only the final variable members of this method can be accessed
}
}
Innertwo innertwo = new Innertwo ();
Innertwo.testprint ();
}
public void use () {
Inner innerobj = new Inner ()//At this time Inner have not seen.
SYSTEM.OUT.PRINTLN (inner.x)//At this time Inner have not seen.
}
public static void Main (string[] args) {
Funouter outer = new Funouter ();
Outer.test ();
}
}
From the above routines we can see that the inner class defined within the method is less visible, and it is only within the method
Visible, in the external class (and other methods of the external class) are not seen. At the same time, it has a feature that is the method
The inner class inside is not accessible to the member variable of this method, it can only access the final member of this method. At the same time another
Attention is drawn to the method's internal definition members, which allow only final or no modifiers, and other such as static are not available.
Five, anonymous internal class
The code shown below defines an anonymous inner class: Anonymous inner classes are typically used in Java event handling
Copy Code code as follows:
Import java.applet.*;
Import java.awt.event.*;
public class Anonymousinnerclassdemo extends applet{
public void init () {
Addmouselistener (New Mouseadapter () {
public void mousepressed (MouseEvent me) {
Showstatus ("Mouse pressed!");
}
})
}
public void Showstatus (String str) {
System.out.println (str);
}
}
In the example above, the method Addmouselistener accepts an object-type parameter expression, so in the argument we define an anonymous inner class, which is a class of mouseadapter types, At the same time, an inherited method mousepressed is defined in this class, and the whole class is a parameter. This class does not have a name, but it is instantiated automatically when the expression is executed. Also, because this anonymous inner class is defined inside the Anonymousinnerclassdemo class, it can access its method showstatus. This is consistent with the previous inner class.
Vi. other problems in the use of internal classes
Through the above, we can clearly see some of the use of internal classes, while, in many cases, internal classes are in the Java event processing, or as a value object to use. At the same time, we need to pay attention to the last problem, that is, the interior is similar to other classes are defined, but also it can inherit the external package of the class and implementation of external interfaces. It can also inherit other inner classes of the same level, and even inherit the outer class itself. Here we give the last example as an end:
Copy Code code as follows:
public class Layer {
Member variables for the layer class
Private String teststr = "Teststr";
//person class, base class
class person{
String name;
email email;
public void SetName (String 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 of
//inner class, 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 inner class inherits the outer class itself
class Childlayer extends layer{
Void print () {
System.out.println (super.test STR)//access to the member variable of the parent class
}
///Another inner class inherits the inner class person
class Officeperson extends person{
Void Show () {
SYSTEM.O UT.PRINTLN (name);
System.out.println (Getemail ());
}
}
////External class test method
public void TestFunction () {
//test First inner class
Childlayer childlayer = new Childlayer (); br> Childlayer.print ();
Testing the second inner class
Officeperson Officeperson = new Officeperson ();
Officeperson.setname ("Abner Chai");
Note here, you must use the object. New object's Subclass object
Rather than person.new Email (...)
Nor is it new person.email (...)
Officeperson.setemail (officeperson.new Email ("Josserchai", "yahoo.com"));
Officeperson.show ();
}
public static void Main (string[] args) {
Layer Layer = new Layer ();
Layer.testfunction ();
}
}