Reprint: http://www.cnblogs.com/dolphin0520/p/3811445.html
Java Internal class
I. Inner-class basis
In Java, a class can be defined inside another class or inside a method, and such a class is called an inner class. The broad range of internal classes generally includes these four types: member inner class, local inner class, anonymous inner class, and static inner class. Let's take a look at the usage of these four inner classes.
1. member Inner class
The member inner class is the most common inner class, defined as being inside another class, as in the following form:
class Circle { double radius = 0; Public Circle (double radius) { this. radius = radius; } class Draw { // inner class public void drawsahpe () { System.out.println ("Drawshape"); }}}
This looks like the class draw is a member of the class circle, called the outer class. Member inner classes have unconditional access to all member properties and member methods of the external class, including private and static members.
classCircle {Private DoubleRadius = 0; Public Static intCount =1; PublicCircle (Doubleradius) { This. Radius =radius; } classDraw {//Inner class Public voidDrawsahpe () {System.out.println (RADIUS); //Private members of external classesSystem.out.println (count);//static members of external classes } }}
It is important to note, however, that when a member inner class has a member variable or method with the same name as an external class, there is a hidden phenomenon, that is, by default, members of the members inner class are accessed. If you want to access a member of the same name as an external class, you need to access it in the following form:
The external class. This . Member variable external class. this. Member method
Although members of the inner class can access the members of an external class unconditionally, the external class would like to access members of the member's inner class but not so much. In an external class, if you want to access members of a member's inner class, you must first create an object of the member's inner class, and then use a reference to that object to visit:
classCircle {Private DoubleRadius = 0; PublicCircle (Doubleradius) { This. Radius =radius; Getdrawinstance (). DRAWSAHPE (); //You must first create an object of the member's inner class and then access } PrivateDraw getdrawinstance () {return NewDraw (); } classDraw {//Inner class Public voidDrawsahpe () {System.out.println (RADIUS); //Private members of external classes } }}
A member inner class exists that is dependent on an external class, that is, if you want to create an object of the member's inner class, as long as an object of an outer class must exist. The general way to create a member's inner class object is as follows:
Public classTest { Public Static voidMain (string[] args) {//The first way:Outter outter =NewOutter (); Outter.inner Inner= Outter.NewInner ();//You must create a Outter object by using the//The second way:Outter.inner Inner1 =outter.getinnerinstance (); }} classOutter {PrivateInner Inner =NULL; Publicoutter () {} PublicInner getinnerinstance () {if(Inner = =NULL) Inner=NewInner (); returninner; } classInner { PublicInner () {} }}
Internal classes can have private access, protected access, public access, and package access. For example, if the member inner class inner is modified with private, it can only be accessed inside the external class, if it is modified with public, it will be accessible anywhere, if it is decorated with protected, it can only be accessed under the same package or inheriting the external class. ; If it is a default access, it can only be accessed under the same package. This is a bit different from the external class, and the outer class can only be decorated with both public and package access permissions. I personally understand that because the member inner class looks like a member of an external class, you can have multiple permissions adornments like members of a class.
2. Local inner class
A local inner class is a class that is defined within a method or scope, and differs from a member's inner class in that the access of a local inner class is limited to within a method or within that scope.
class people{ public people () {}} class man{ public Man () {}
public people Getwoman () { class Woman // Span style= "color: #008000;" > local inner class int age =0< span> return new Woman (); }}
Note that the local inner class, like a local variable inside a method, cannot have public, protected, private, and static modifiers.
3. Anonymous inner class
An anonymous inner class cannot have an access modifier and a static modifier, and an anonymous inner class cannot be abstract, it must implement all the abstract methods of an inherited class or an implemented interface.
Anonymous inner classes are the only classes that do not have constructors. Because it does not have a constructor, the use of anonymous internal classes is very limited, and most anonymous inner classes are used for interface callbacks.
Anonymous inner classes are automatically named Outter$1.class by the system at compile time. In general, anonymous inner classes are used to inherit other classes or implement interfaces, but both cannot be combined and can only inherit one class or implement an interface, and there is no need to add additional methods, just the implementation of inherited methods or overrides.
Anonymous inner class initialization: Use to construct blocks of code! The effect of creating a constructor for an anonymous inner class can be achieved by building blocks of code
In our daily use of the most anonymous inner class is in the listener, the following code is an Android event listener code:
Scan_bt.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub } }); History_bt.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub }});
which
New Onclicklistener () { @Override public void OnClick (View v) { //TODO auto-generated method Stub }});
is the implementation of an anonymous inner class
Of course, such as the following is also possible, with the above using anonymous inner class to achieve the same effect.
Private voidSetlistener () {Scan_bt.setonclicklistener (NewListener1 ()); History_bt.setonclicklistener (NewListener2 ());} classListener1Implementsview.onclicklistener{@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub }} classListener2Implementsview.onclicklistener{@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub }}
Although this kind of writing can achieve the same effect, but it is both lengthy and difficult to maintain, it is common to use anonymous inner class methods to write event monitoring code.
4. Static Inner class
A static inner class is also a class that is defined in another class, except that there is a keyword static in front of the class. A static inner class is not dependent on an external class, which is somewhat similar to the static member property of the class, and it cannot use the non-static member variable or method of the outer class, which is well understood,
Because in the absence of an object of an outer class, you can create an object of a static inner class (because the normal inner class needs to be created outside the object before it needs to create the object), and if you allow access to the non-static members of the outer class, you will have a contradiction. Because a non-static member of an external class must be attached to a specific object.
Public class Test { publicstaticvoid main (string[] args) { new Outter.inner (); class outter { public outter () { } staticclass Inner { public Inner () { } }}
Not to be continued
Java Internal classes