Summary:
1. The first benefit of the internal class is to hide the operations you don't want others to know, that is, encapsulation.
2. An internal class object can access the content of its external class object, and even include private variables! This is a very useful feature,
Java uses the following format to express external class references:
Outerclass. This
3. Static internal classes can only access static variables or methods of static external classes
4. Anonymous internal class creation:
New interfacename () {...}; or new superclassname (){......};
5. The anonymous internal class inherits a parent class that only contains the parameter constructor. when creating the class, these parameters must be included and the corresponding content must be called using the super keyword during implementation. If you want to initialize its member variables, you can use the following methods:
If it is in an anonymous internal class of a method, you can use this method to pass in the desired parameters, but remember that these parameters must be declared as final.
The anonymous internal class is transformed into a local internal class with a name, so that it can have constructors. Use the initialization code block in this anonymous internal class.
Many people may not be familiar with the inner class mentioned in Java. In fact, similar concepts also exist in C ++, that is, nested class ), the differences and connections between the two are compared in the following section. On the surface, the internal class defines another class in the class (as we can see below, the internal class can be defined in many places), but it is actually not that simple, at first glance, the internal class seems redundant. Its usefulness may not be so significant for beginners, but with its in-depth understanding, you will find that Java designers have a good heart for internal classes. Learning to use internal classes is part of advanced Java programming. It allows you to design your program structure more elegantly. The following describes how:
First meeting
Public interface contents {<br/> int value (); <br/>}< br/> Public interface destination {<br/> string readlabel (); <br/>}< br/> public class goods {<br/> private class content implements contents {<br/> private int I = 11; <br/> Public int value () {<br/> return I; <br/>}< br/> protected class gdestination implements destination {<br/> private string label; <br/> private gdestination (string whereto) {<br/> label = whereto; <br/>}< br/> Public String readlabel () {<br/> return label; <br/>}< br/> public destination DEST (string s) {<br/> return New gdestination (s ); <br/>}< br/> Public contents cont () {<br/> return new content (); <br/>}< br/> class testgoods {<br/> Public static void main (string [] ARGs) {<br/> goods P = new goods (); <br/> contents c = P. cont (); <br/> destination d = P. deST ("Beijing"); <br/>}< br/>}
In this example, the class content and gdestination are defined inside the class goods and have the protected and private modifiers respectively to control the access level. Content represents goods content, while gdestination represents goods destination. They implement the content and destination interfaces respectively. In the following main method, you can directly use contents C and destination d to perform operations. You don't even see the names of these two internal classes! In this way, the first benefit of the internal class is to hide the operations you don't want others to know, that is, encapsulation.
At the same time, we also found that the first method to get internal class objects out of the scope of the external class is to create and return the internal class objects using its external class method. The cont () and DEST () methods in the preceding example do this. Is there any other way? Of course, the syntax format is as follows:
Outerobject = new outerclass (constructor parameters );
Outerclass. innerclass innerobject = outerobject. New innerclass (constructor parameters );
Note that when creating a non-static internal class object, you must first create an external class object. As for the reason, it leads to our next topic that non-static internal class objects have references pointing to their external class objects. I will slightly modify the example just now:
Public class goods {<br/> private int valuerate = 2; <br/> private class content implements contents {<br/> private int I = 11 * valuerate; <br/> Public int value () {<br/> return I; <br/>}< br/> protected class gdestination implements destination {<br/> private string label; <br/> private gdestination (string whereto) {<br/> label = whereto; <br/>}< br/> Public String readlabel () {<br/> return label; <br/>}< br/> public destination DEST (string s) {<br/> return New gdestination (s ); <br/>}< br/> Public contents cont () {<br/> return new content (); <br/>}< br/>}
Here we add a private member variable valuerate to the goods class, which indicates the value coefficient of the goods. The value () of the Content Method of the internal class is multiplied when it calculates the value. We found that value () can access valuerate, which is the second advantage of the internal class. An internal class object can access the content of the external class object that creates it, and even include private variables! This is a very useful feature that provides us with more ideas and shortcuts during design. To implement this function, internal class objects must have references pointing to external class objects. When creating an internal class object, the Java compiler implicitly transfers the reference of its external Class Object and keeps saving it. In this way, internal class objects can always access their external class objects. This is also why external class objects must be created before they are created outside the scope of the external class.
Someone may ask, what if a member variable in the internal class has the same name as a member variable in the external class, that is, the member variable with the same name in the external class is blocked? Nothing. Java uses the following format to express external class references:
Outerclass. This, with it, we are not afraid of such blocking.
Static internal class
Like normal classes, internal classes can also be static. However, compared with non-static internal classes, the difference is that static internal classes do not point to external references. This is actually very similar to the nested classes in C ++. The biggest difference between Java internal classes and C ++ Nested classes is whether there is a reference pointing to the outside, of course, there are still differences from the design perspective and some details.
In addition, no non-static internal class can have static data, static methods, or another static internal class (internal class nesting can be more than one layer ). However, static internal classes can have all of this. This is the second difference between the two.
Local internal class
Yes, Java internal classes can also be local, which can be defined in a method or even a code block.
Public class goods1 {<br/> public destination DEST (string s) {<br/> class gdestination implements destination {<br/> private string label; <br/> private gdestination (string whereto) {<br/> label = whereto; <br/>}< br/> Public String readlabel () {<br/> return label; <br/>}< br/> return New gdestination (s ); <br/>}< br/> Public static void main (string [] ARGs) {<br/> goods1 G = new goods1 (); <br/> destination d = G. deST ("Beijing"); <br/>}</P> <p> This article is from the csdn blog, reprinted please indicate the source: http://blog.csdn.net/dingxy/archive/2011/02/09/6175333.aspx #
The above is an example. In the Dest method, we define an internal class. Finally, this method returns the object of this internal class. If we only need to create an object and create it to the external when using an internal class, we can do this. Of course, the internal class defined in the method can diversify the design, and its purpose is not only in this regard. The following is a more strange example:
Public class goods2 {<br/> private void internaltracking (Boolean B) {<br/> If (B) {<br/> class trackingslip {<br/> private string ID; <br/> trackingslip (string s) {<br/> id = s; <br/>}< br/> string getslip () {<br/> return ID; <br/>}< br/> trackingslip Ts = new trackingslip ("slip"); <br/> string S = ts. getslip (); <br/>}< br/> Public void track () {<br/> internaltracking (true ); <br/>}< br/> Public static void main (string [] ARGs) {<br/> goods2 G = new goods2 (); <br/> G. track (); <br/>}</P> <p> this article from the csdn blog, reprinted please indicate the source: http://blog.csdn.net/dingxy/archive/2011/02/09/6175333.aspx #
You cannot create an internal Class Object outside of if because it is beyond its scope. However, during compilation, the internal class trackingslip is compiled at the same time as other classes, except that it is in its own scope and is invalid if it exceeds this scope, it is no different from other internal classes.
Anonymous internal classThe syntax rules for anonymous internal classes in Java seem odd, but like an anonymous array, when you only need to create a Class Object and cannot use its name, using Internal classes can make the Code look concise and clear. Its syntax is as follows: New interfacename (){......}; or New superclassname (){......}; next we will continue with the example above:
Public class goods3 {<br/> Public contents cont () {<br/> return new contents () {<br/> private int I = 11; <br/> Public int value () {<br/> return I; <br/>}< br/> }; <br/>}< br/>
Here, the cont () method uses an anonymous internal class to directly return an object of the class implementing the contents interface, which looks very concise. In the anonymous adapter for Java event processing, anonymous internal classes are widely used. For example, add the following code when you want to close the window:
Frame. addwindowlistener (New windowadapter () {<br/> Public void windowclosing (invalid wevent e) {<br/> system. exit (0); <br/>}< br/>}); <br/>One thing to note is that the anonymous internal class has no name, so it does not have a constructor (but if this anonymous internal class inherits a parent class that only contains a parameter constructor, these parameters must be included during creation and the corresponding content must be called using the super keyword during implementation ). If you want to initialize its member variables, you can use the following methods:
If it is in an anonymous internal class of a method, you can use this method to pass in the desired parameters, but remember that these parameters must be declared as final.
The anonymous internal class is transformed into a local internal class with a name, so that it can have constructors.
Use the initialization code block in this anonymous internal class.
What are the advantages of Java internal classes? Why Internal classes?
First, let's take a simple example. If you want to implement an interface, but a method in this interface has the same name and parameter as a method in the class you have conceived, what should you do? In this case, you can create an internal class to implement this interface. Since all the content of the internal class is accessible to external departments, this can complete all the functions that you can directly implement this interface.
But you may have to question it. Isn't it enough to change the method? Indeed, it is unconvincing to use this as a reason to design internal classes.
The real reason is that the combination of internal classes and interfaces in Java can solve a problem that C ++ programmers often complain about in Java: there is not much inheritance. In fact, the multi-inheritance design of c ++ is very complicated, and Java can implement multi-inheritance effectively by adding interfaces to internal classes.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/dingxy/archive/2011/02/09/6175333.aspx #