First Know Java Internal Class Original: Morgan83
Lift Java Inner Class May Many people are not familiar, in fact, similar concepts are also in C , that is, nested class, the difference between the two, which will be compared in the following. Internal classes look from the surface, that is, a class is defined in the class (hereinafter seeing, the internal class can be defined in many places), and actually is not as simple, suddenly it seems to be a little excess, its It may not be so significant for beginners, but with the in-depth understanding of it, you will find that Java designer is indeed good to work in internal classes. Learn to use internal classes to master part of Java's advanced programming, which allows you to design your program structure more elegant. The following are the following aspects:
First meeting
public interface Contents {int value ();} public interface Destination {String readLabel ();} public class Goods {private class Content implements Contents {private int i = 11; public int value () {return i;}} protected class GDestination implements Destination {private String label; private GDestination (String whereTo) {label = whereTo;} public String readLabel () {return label;}} public Destination dest (String s) {return new GDestination (s);} public Contents cont ( ) {Return new content ();}} class testgoods {public static void main (string [] args) {goods p = new goods (); contents c = p.cont (); destination d = p.dest ("beijing ");}} In this example, CONTENT and GDestination are defined inside the class goods and have protected and private modifiers to control access levels. Content represents the content of Goods, while GDestination represents the destination of Goods. They achieve two interfaces Content and Destination respectively. In the later main method, use CONTENTS C and DESTINATION D to operate, and even the names of these two internal classes have not seen it! In this way, the first advantage of the internal class is reflected - hiding you don't want others to know the operation, that is, encapsulation. At the same time, we also found the first method of getting internal objects outside the externally role range, which is created and returned by the method of its external class. The CONT () and DEST () methods in the above example are done. So there is any other way? Of course, its grammar format is as follows:
OuterObject = New OuterClass (Constructor parameters); OuterClass.innerClass InnerObject = OuterObject.new InnerClass (Constructor parameters); Note When you create a non-static internal class object, you must create a corresponding external class object first. As for the reasons, we will also lead to our next topic - non-static internal class objects have a reference to its external class objects to modify the examples of just now:
public class Goods {private valueRate = 2; private class Content implements Contents {private int i = 11 * valueRate; public int value () {return i;}} protected class GDestination implements Destination {private String label; private GDestination (String whereTo) {Labl = WHERETO;} public string readlabel () {return label;}} public destination dest (String s) {return new gdestination (s);} public contents last () {return new content ();}} Displayed with blue. Here we add a private member variable value value to the Goods class, which is the value coefficient of the goods, multiplied it when the internal CONTENT method value () calculates the value. We found that value () can access valuerate, which is the second benefit of internal classes - an internal class object can access the content of the created external class object, even private variables! This is a very useful feature that provides more ideas and shortcuts for us. To implement this feature, the internal class object must have a reference to the external class object. When you create an internal class object, the Java compiler is implicitly passed in and has been saved. This allows the internal class object to always access its external class object, and this is why the external class object must be created in the external class scope must first create the reason why the external class object is created. Some people will ask, if a member variable in the internal class is the same name, that is, the external class member variable is blocked, what should I do? Nothing, Java uses the following format to express the external class reference:
Outerclass.this has it, we are not afraid of this shield.
The static internal class and ordinary class, the internal class can also be static. However, compared with non-static internal classes, the difference is that the static internal classes have no references to the outside. This is actually very similar to the nested classes in C , the biggest difference between the Java internal classes and C nested classes is whether there is a reference to the outside, of course, from the perspective of the design, and some details Difference. In addition, in any non-static internal class, there is no static data, static method, or still another static internal class (nesting of the internal class, more than one layer). However, the static internal class can have all this. This is also the second difference between the two.
The local internal class is, the Java internal class can also be partial, which can be defined within one method or even a block. public class Goods1 {public Destination dest (String s) {class GDestination implements Destination {private String label; private GDestination (String whereTo) {label = whereTo;} public String readLabel () {return label;}} return new GDestination (s) Public static void main (String [] args) {goods1 g = new goods1 (); destination d = g.dest ("beijing");}} The above is the example. In the method DEST we define an internal class, and finally the object returns to this internal class by this method. If we only need to create an object and create it to the outside when using an internal class, you can do this. Of course, the internal classes defined in the method can make design diversification and use is not just at this point. There is a spoken example below:
public class Goods2 {private void internalTracking (boolean b) {if (b) {class TrackingSlip {private String id; TrackingSlip (String s) {id = s;} String getSlip () {return id;}} TrackingSlip ts = new TrackingSlip ("SLIP"); string s = ts.getslip ();}} public void track () {INTERNALTRACKING (TRING ";} public static void main (string [] args) {goods2 g = new goods2 (); g. You cannot create this internal class object outside of the IF because it has exceeded its scope. However, in the compilation, internal class trackingslip is compiled simultaneously, but it is ineffective by its own scope, which is exceeded, in addition to this and other internal classes.
The symbol rules of anonymous internal classes of anonymous internal classes look like some weird, but like an anonymous array, when you only need to create a class object and use the internal class, use the internal class to make the code look simple and clear. . Its syntax rules are like this:
NEW interfacename () {...}; or new superclassname () {...}; the next surface continues to exemplify:
Public Class Goods3 {public CONTENTS () {Return New Contents () {Private INT i = 11; Public INT Value () {RETURN I;}};}} here Method CONT () Use an anonymous internal class directly to return a implementation The object of the class of the interface Contents, it seems to be very simple. In an anonymous adapters for Java's event processing, anonymous internal classes are used in a large number of uses. For example, when you want to close the window: frame.addwindowlistener (new windowadapter () {public void windowclosing (windowevent e) {system.exit (0);}}); one thing to note is that anonymous internal classes Due to no name, it doesn't construct a function (but if this anonymous internal class inherits a parent class containing only parameter constructor, you must bring these parameters when creating it, and use Super Key to call during the implementation Corresponding content). If you want to initialize its member variable, there are several ways:
If it is an anonymous internal class in a method, you can use this method to pass into the parameters you want, but remember that these parameters must be declared as Final. Transform anonymous internal class into a local internal class of the name so that it can have a constructor. In this anonymous internal class, the initialization code block is used.
Why do I need internal classes? What is the benefits of the internal classes of the Java? Why do I need internal classes? First, give a simple example, if you want to implement an interface, a method in this interface and the name of a method in this class, the parameter, what should you do? At this time, you can build an internal class to implement this interface. Since all content of the internal class is accessible to the external class, do this can do all the functions you directly implement this interface. However, you may have to question, can you change the way? Indeed, in this regard as the reason for designing internal classes, there is really no persuasion. The real reason is that in this case, the internal classes and interfaces in Java add together, and can solve a problem that is often complained in Java by C programmers - there is not much inheritance. In fact, C 's multi-inheritance is complex, while Java adds an interface through internal classes, it can achieve more inheritance effects. The purpose of this article is to introduce you to the concept of internal classes and how to use, in subsequent articles, will be more specific examples in this article, and how to build an Applicaton Framework using internal classes.