Inner Class Inner Class (Inner Class) Many people may not be familiar with, in fact, similar concepts are also in C , that is, nested class, about the difference between the two, under There will be comparison in the text. 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 aspects are introduced: • First meeting public interface contents {int value ();} public interface destination {string readlabel (); public class goods {private class const portments {private id 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 (} public contents last () {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, class 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, syntax is as follows: outerObject = new outerClass (Constructor Parameters); outerClass.innerClass innerObject = outerObject.new InnerClass (Constructor Parameters); Note When creating a non-static inner class object, you must first create the corresponding external from the class Object. As for the reasons, we will lead to our next topic - · Non-static internal class objects have references to their external classes to modify their exemplary examples: public class goods {private value = 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) {label = whereTo;} public String readLabel () {return label; }} Public destination dest (String s) {return new gdestination (s);} public contents last () {return new content ();} The changed section is displayed in 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 classes: OuterClass.this has it, we are not afraid of this shielding situation. · 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 split example: public class goods2 {private void inloading (boolean b) {if (b) {class; trackingslip (string s) {id = s;} string getslip ()} String getSlip () {Return ID }}}} Trackingslip ("slip"); string s = ts.getslip ();}} public void track ()}} public void track ()}}} public static void main (String [] args) {goods2 g = New Goods2 (); g.track ();}} You cannot create objects of this internal class outside of 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 have a certain quirky, 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. clear. Its syntax rules are like this: new interfacename () {...}; or new superclassname () {...}; below, continue to exemplify: public class goods3 {public contents last ) {RETURN New Contents () {Private INT i = 11; public int value () {return i;}};}} here Method CONT () uses an anonymous internal class directly to a class that implements the classes 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: 1. If it is an anonymous internal class in a method, you can use this method to pass the parameters you want, but remember that these parameters must be declared as Final. 2. Transform anonymous internal classes into a local internal class of name so that it can have a constructor. 3. Use the initialization code block in this anonymous internal class. · Why do you 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. Source: 9CBS Date: 2004-10-29