Objective
Learning Java has been a long time, the recent days of the author in the Java again to learn. But this stage of learning
I will not focus on the specific grammar of what the details of the things, this stage of the study of the author of the previous study
Vague, omitted knowledge of the concept of doing some relevance to the summary. Today, just see the inner class this piece, remember the previous to the inner class
The use of a number of fuzzy, so specifically on the internal class to do some summary. Internal class Concepts
An inner class is the definition of a class (inner class) in an external class, so that the inner class is attached to the
External class. However, when using the inner class, it is necessary to note that the inner class can be static,protect,private, but
The external class can only use public and default package access permissions.
If either of these compile errors:
Illegal modifier for the class Outer; Only public, abstract & final are permitted
Package com.kiritor;
public class Outer {
private String type;
Public String GetType () {return
type;
}
public void SetType (String type) {
this.type = type;
}
Class Inner {
private String type;
Public String GetType () {return
type;
}
public void SetType (String type) {
this.type = type;
}
}
}
the meaning of the inner class
In simple terms, the inner class seems like a code-behind mechanism: the class is internal to other classes. But the inner class is much more than that.
It understands the external class and can communicate the necessary communication with the external class.
1, encapsulation Some people do not want to know the operation.
2. The inner class can access the contents of the external class object that created it, and even include the variable of private.
The inner class can also implement interfaces, inheriting base classes, which makes multiple inheritance in Java complete. We can use the inner class
Way to simulate the effect of multiple inheritance.
By inheriting the base class from the inner class, the outer class creates the inner class object and uses the method provided by the inner class, which in disguise implements the
The effect of multiple inheritance.
public class graduate{
private Graduate_stu gaduate_stu= new Graduate_stu ();
Private graduate_emp graduate_emp = new Graduate_emp ();
Private class Graduate_stu extends student{public
void GetName () {
...
}
}
Private class Graduate_emp extends employee{public
double Getmoney ()
{return
0.0;
}
}
public void GetName () {
gaduate_stu.getname ();
}
Public double Getmoney () {
graduate_emp. Getmoney ();
}
}
Just a simple simulation with the code!
An attractive feature of the inner class is that the inner and outer classes are "detached" for the inheritance of the interface.
Mutually unaffected, based on the actual situation, sometimes we implement an interface, but the method of the interface in this class
is already defined in this case, we can use the inner class to implement the interface and implement its method because the inner
The class is accessible to members of the external classes, so the method of using the inner class solves the problem. Classification of internal classes
We now know that the inner class is placed in the outer class, depending on the "position" and the attributes of the inner class, the inner class can
Divided into the following categories:
member Inner class
Local inner class
Static inner class (nested Class)
Anonymous inner class
For its specific features and usage, the member inner class is described below
An inner class exists as a member of an outer class, and is tied to the methods of the outer class.
Package com.kiritor;
public class Outer {private String type= "Outer class";
private static int flag = 1;
Public String GetType () {return type;
public void SetType (String type) {this.type = type;
@Override public String toString () {System.out.println ("" +gettype () + ":" +this.flag);
Inner Inner = new Inner ();
Inner.tostring ()///The Non-static method of the outer class accesses the method of the inner class//The static method access of the outer class is the same, not demonstrated.
return super.tostring ();
Class Inner {private String type= "Inner class";
private int flag=2;//This is not allowed in member inner class to define static member public String GetType () {return type;
public void SetType (String type) {this.type = type;
Public String toString () {System.out.println ("" +gettype () + ":" +this.flag);
System.out.println ("" "+outer.this.gettype () +outer.this.flag);//If there is a variable name, this way to access return super.tostring ();
} public static void Main (string[] args) {Outer Outer = new Outer ();
Outer.tostring (); Outer.Inner Inner = outer.new Inner ()//new Inner by this way Inner. toString ();
}
}
Tips: When creating an inner class, it is not possible to generate an external class object unless you already have one.
Method internal class object, because the inner class object will silently link to the object that created his outer class, no external class object
Naturally, it is not possible to generate internal class objects, but it is also necessary to note that the internal class is a compile-time concept, once compiled
Through, it becomes a completely different class of two, which means that the Outer,class and Outer$inner,class two byte codes appear.
File.
local inner class
The inner class defined in a method is called a local inner class. It is similar to a local variable, so the local inner class is inaccessible
modifier, because it is not an external class member, but he can access the constants of the code block in the current method, and all of the outer classes
Members.
Package com.kiritor;
public class Outer {private String type = "Outer class";
private static int flag = 1;
Public String GetType () {return type;
public void SetType (String type) {this.type = type;
@Override public String toString () {System.out.println ("" + getType () + ":" + This.flag);
return super.tostring ();
The public void Innerinfo () {final String innerfinal = "constant that can access the method body";
Class Inner {private String type = "Inner class";
private int flag = 2;//This member inner class does not allow the definition of static member public String GetType () {return type;
public void SetType (String type) {this.type = type;
Public String toString () {System.out.println ("" + getType () + ":" + This.flag + innerfinal);
System.out.println ("" + Outer.this.getType () + Outer.this.flag);//If there is a variable name, this way to access return super.tostring ();
} new Inner (). toString ()//Note the method of calling the inner class in this way!
public static void Main (string[] args) {Outer Outer = new Outer (); Outer.tosTring ();
Outer.innerinfo ();
}
}
static inner class (nested Class)
The first two kinds of internal classes and variables are similar, where the variables are member variables, and local variables, you can refer to the comparison
If you don't need a connection between the inner class object and its outer class object, you can declare the inner class as static, which
is the static inner class. What we need to understand is that ordinary internal class objects implicitly hold a reference to an external class object.
But when the inner class is static, this "feature" is gone, which means:
1, the creation of static internal class objects do not need the external class object
2. Non-static external class objects cannot be accessed through static internal class objects.
See Example:
Package com.kiritor; public class Outer { private String type = "Outer class"; private static int flag =
1; public String GetType () { return type; } &nbs p; public void SetType (String type) { this.type = type; &nb
SP;} @Override public String toString () {
System.out.println ("" + getType () + ":" + This.flag); inner.info ()///external class Access static members of internal classes: internal classes. Static member (static method) inner Inner = new Inner ();//This generates an internal class object no longer needs to be passed through the external class object inner.tostring ();
An external class accesses a Non-static member or method of a static inner class that must be new to an object system.out.println (inner.type);
return super.tostring (); } staTic class Inner { private String type = "Inner class"; &nbs P
private int flag = 2;
private static String info= "Inner Class 2"; //Static internal classes can have non-static methods, properties public String GetType () {
return type; } public void SetType (String type) {
This.type = type; } public static void info () { system.out.println (info); } Public String toString () { System.out.println ("" + getType () + ":" + this.flag
); //system.out.println ("" + Outer.gettype ( + Outer.this.flag);//If there is a variable name, this way access
return super.tostring (); } } public static void Main (string[] args) { outer Outer = new Outer (); &NB sp; outer.tostring ();
}}
As you can see, building an internal class object no longer requires an external class object, which is also a static inner class and
The difference between a member's inner class: Outer. Inner in = new Outer.Inner ();
Anonymous Inner class
Simply put, anonymous inner class is a class without a name, which is more common in GUI programming, giving an example:
package com.kiritor;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseListener;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
public class MyFrame extends jframe{private JButton button = null;
Public MyFrame () {this.setsize (200, 200);
This.setvisible (TRUE);
button = new JButton ("Anonymous inner class"); Button.addmouselistener (New MouseListener () {//An anonymous class @Override public void mousereleased (MouseEvent e) {// Todo auto-generated method stub} @Override public void mousepressed (MouseEvent e) {//Todo auto-g enerated method stub} @Override public void mouseexited (MouseEvent e) {//TODO auto-generated Meth
OD stub} @Override public void mouseentered (MouseEvent e) {//TODO auto-generated method stub
@Override public void mouseclicked (MouseEvent e) {//TODO auto-generated Method stub}});
This.add (button); }
}
For anonymous internal class, the author is not done summary, then will find a time to understand.
issues related to internal classes
Here is a discussion of some interesting questions in the inner class!
Can the inner class be overloaded, inherited? internal class overload problem
Suppose you create an outer class and define an inner class, then inherit the outer class and redefine the inner class
What happens when you do that?
Package com.kiritor;
Class Outer {public
Outer () {
System.out.print ("Outer:");
New Inner ();
}
Class Inner {public
Inner () {
System.out.println ("Inner");
}} public class Outer2 extends Outer {
class Inner {public
Inner () {
System.out.println ("Outer2:inner"); c15/>}
} public
static void Main (string[] args) {
new Outer2 ();
}
}
Take a look at the output: Outer:inner
The default constructor Outer2 () is generated automatically by the compiler, and he will call the constructor of the parent class, and the result can be seen
Although the subclass object is created, the inner class is not an "overload" that is used. This means that you inherited an external class.
, the inner class does not have special changes, of course, the explicit inheritance of an internal class except in the way!
Package com.kiritor;
Class Outer {public
Outer () {
System.out.print ("Outer:");
New Inner ();
}
Class Inner {public
Inner () {
System.out.println ("Inner");
}} public class Outer2 extends Outer {
class Inner extends com.kiritor.outer.inner{public Inner
() {
System.out.println ("Outer2:inner");
}
Public Outer2 () {
new Inner ();
}
public static void Main (string[] args) {
new Outer2 ();
}
}
The output after an explicit inheritance is:
inheritance issues for internal classes
Sometimes we just need to inherit the inner class, but the constructor of the inner class must also use the reference of the external object
, so it's a bit special to inherit an inner class, and the main problem is that the reference to an external class object must
initialization, and in the inherited class does not exist, that is, the single inheritance of internal classes, there is no internal class and its external class
An association.
You can use the method to resolve:
Package com.kiritor;
Import Com.kiritor.Outer.Inner;
Class Outer {public
Outer () {
System.out.print ("Outer:");
New Inner ();
}
Class Inner {public
Inner () {
System.out.println ("Inner");
}} public class Inner2 extends Outer.Inner {
Inner2 (Outer Outer)
{
outer.super ();
The constructor can only be System.out.println in this way
("only for this type of builder");
}
public static void Main (string[] args) {
new Inner2 (New Outer ());
}
The output results are:
Outer:inner
Inner
Only for this type of builder
As you can see, INNER2 only integrates the inner class, but its default constructor is not available and simply passes a
The reference to the outer class is not enough, and you must first call the construction method of the outer class. This provides an internal class and an external class object
Can be compiled by using a reference association.