Abstract: In JDK1.1, the most controversial changes may be introduced into the "inner embedding class ". The root cause of the debate lies in the fact that a large number of people recognize it as an embedded class, which will be changed to the JAVA language, making it swollen. However, I think the nested class is an effective change to the JAVA language, and there is no effect on the existing JAVA virtual machine.
In the topic titled "General statement" in the December 1996 s, I discussed the difficulty of constructing a set of classes in JAVA. Two things happened later: 1. SUN created an inner-embedded class to help implement java beans; 2, SUN provides a basic set class for JAVA. I like SUN's suggestion, but I hope SUN can provide a method for performing strong-class inspection. The strong-class inspection is a kind of machine system with the same type of images that are saved to a certain set combination. Before, these sets use Object classes as the base Object, if you mistakenly save a non-method class into a set by mistake in your class, then, when you search for the cited data from the set combination, the above cited data will generate a class of strong equivalence. As an example of a class that is strongly-typed for reference. See the "ContainerOrganizer" Class in the "resource source" section.
1. What is an embedded class?
An inner-embedded class is a class file whose names are "Limited" in another class. The so-called "limitation" defines when it can be cited by JAVA code generation. When two classes share a package, they have a "package limit ". Unless the non-package is defined as a public package, otherwise the classes outside the package cannot be referenced in it. This class name is limited to the package.
The nested class is limited to the class that explains it. In reality, its class in the same package is invisible. This screen shielding of other classes in the package enables programmers to create class sets in the included classes of the package. At the same time, it will not confuse the empty life names in the package.
According to SUN's article, SUN's engineers considered the inner embedded class to solve a question in the JAVA definition: JAVA classes can only be defined as the same class as other classes. The inner-embedded class first changes to the editor and implements backward synchronization between support and existing virtual machines, the embedded class is compiled into a regular class file, and the name is changed to prevent conflict with the existing class.
To support the inner embedded class, in JAVA, the related rules of the class that should be stated in the Process Order are modified. For example, if you have compiled the process order in Java 1.0x, you will know that you can define multiple classes in a single source file (only one of them is public ), and the second and subsequent classes must be closely followed by the Ampersand of the first class. In 1.1, all of these changes: Now you can declare the class in another class. The examples below are clearer. In Java, you must repeatedly declare two classes in a file, as shown in the following code:
Public class MyPublicClass {
Public MyPublicClass (){...}
Void method1 (){...}
Int method2 (){...}
Public method3 (int x, int y ){...}
}
The class above is the "base class" stored in the MyPublicClass. java file ). In the method3 method, it needs other classes to help it complete some of its functions. Generally, this class is defined in the same file. The following is an example:
Class MyHelperClass {
MyHelperClass (){...}
Int someHelperMethod (int z, int q ){...}
}
Classes in the same file are usually interconnected on some aspects. For example, we have set up the BSTEnumerator class in the BinarySearchTree class, which provides a back-to-Binary Search Tree with elements (or keys) method. In the subclasses of the Dictionary class, such as Hashtable, these classes are usually grouped into a file.
With the nested class, you can use the following code to describe the class:
Public class MyPublicClass {
Public MyPublicClass (){...}
Class MyHelperClass {
MyHelperClass (){...}
Int someHelperMethod (int z, int q ){...}
}
Void method1 (){...}
Int method2 (){...}
Public method3 (int x, int y ){...}
}
As you can see, this involves only the changes of some texts. However, the appearance of Nested classes is quite meaningful.
The nested class in the above example is MyHelperClass, but there are two classes in the first case (or in the bag that is recognized by the user ): myPublicClass and MyHelperClass. In the second example, there are still two classes. Except for the MypublicClass class, the other class ing is MyPublicClass $ MyHelperClass. In this case, a new name space is created, which only contains the name of the helper class. In the previous example, both the base class and helper class occupy the space of the Life name. With the nested class, the helper class has a complete empty Name Belonging to itself.
2. How to Use the nested class?
We use the BinarySearchTree class to describe the usage of embedded classes.
Import java. io. PrintStream;
Import java. util. Enumeration;
Import java. util. NoSuchElementException;
Import java. util. Dictionary;
Public class BinarySearchTree extends Dictionary {
BSTNode rootNode;
Private int elementCount;
Private ContainerOrganizer co;
/**
* Define an inner class to be the enumerator
*/
Class BSTEnumerator implements Enumeration {
Private BSTNode currentNode;
Private boolean keys;
BSTEnumerator (BSTNode start, boolean doKeys ){
Super ();
CurrentNode = (start! = Null )? Start. min (): null;
Keys = doKeys;
}
Public boolean hasMoreElements (){
Return (currentNode! = Null );
}
Public Object nextElement (){
If (currentNode = null)
Throw new NoSuchElementException ();
BSTNode n = currentNode;
CurrentNode = n. successor ();
Return (keys? N. key: n. payload );
}
}
When compiling the class on JDK1.1, the compiler creates two types of files: BinarySearchTree. class and BinarySearchTree $ BSTEnumerator. class. class. Note that the class name contains the "$" character, which separates the embedded class from the base class containing it. After the inner embedding class BSTEnumerator is defined, the BinarySearchTree can be further defined:
Public BinarySearchTree (ContainerOrganizer c ){
Super ();
Co = c;
Co. setDict (this );
}
... And so on for the rest of the class...
}
Use the main price value of the nested class in this form to create a Special Enumeration class, it does not confuse empty names at the package level. Considering that many different return values based on rows can be returned to a set-and-class group package (for example, a sequential column table, fixed, etc.), with a large number of classes. You only need one single value before using multiple words. However, you can use a hand to complete the O & M work between these empty names. The actual effect is similar to the built-in effect; that's why SUN shengming is only a modifier in the interpreter.
Three built-in classes: Anonymous class
When you only want to deliver a method to do something, you do not need a name (similar to the number of callback letters in C language ), you only need to create an nested class with the name of the slave. These classes are only built-in classes without specific names. Use only one class without a name.
In order to explain the class name, we have compiled the keys and elements methods of the BinarySearchTree. They are all used to return a BSTEnumerator object, as shown below.
Private Enumeration doEnumerate (final boolean k ){
Return new Enumeration (){
Private BSTNode currentNode = rootNode;
Private boolean keys = k;
Public boolean hasMoreElements (){
Return (currentNode! = Null );
}
Public Object nextElement (){
If (currentNode = null)
Throw new NoSuchElementException ();
BSTNode n = currentNode;
CurrentNode = n. successor ();
Return (keys? N. key: n. payload );
}
};
}
Public Enumeration elements (){
Return doEnumerate (false );
}
Public Enumeration keys (){
Return doEnumerate (true );
}
Look at the number of doEnumerate letters in detail. There are two strange cases. First, it returns to the new instance of the interface; second, the parameter k before it is final type. What is the last thing? The answer is that this technique is another method that uses the inner embedding class.
In retrospect, we can see that a new socket instance has been created, however, in practice, a new class is created that does not have a clear name and is connected. In this case, the real name is BinarySearchTree $1, but it is only meaningful to the interpreter.
Use this