Optimize Java code using internal and anonymous classes

xiaoxiao2021-03-06  25

Java 1.1 has significantly simplified some practical structures by modifying the Java language specification. In those modifications, the most striking is the internal class and anonymous classes. If you use it, they make the program more easily understood and maintained. Let's take a look at how these features work, how to use them correctly, and how to avoid some common errors.

Internal classes, "internal classes" are classes in the internal declaration of another class. Starting from Java 1.1, you can declare another class in a class, which is very similar to the declaration fields and methods. Classes packaging the internal declarations are called "external classes". In fact, the Java language specification also allows you to do more, including: declaring a class in another class or an interface. Declare an interface in another interface or a class. Declare a class in a method. Class and interface declarations can be nested with any depth. Listing A is some blank declarations of classes and interfaces, which demonstrates these possibilities. Using an Import statement, you can omit the package name like any other standard class. In addition, in an external class, a simple name can be utilized to reference all internal classes and interfaces (see NEW statements in Listing A). Note that INTER2 is specified from the Inner2 from Method1, because Inner2 is on a different level. Table A summarizes the fully qualified name of each internal class and interface declared in List A. After using the Import statement, shorter forms can be used. Of course, in the external class, you can also omit the names of the external class. Name Class / Interface Inner1 MyPackage.inner1 Interface1 MyPackage.Interface1 INNER2 MyPackage.Interface1.inner2 interface2 mypackage.interface1.interface2 inner3 inner3 is LOCAL for Method1, so it cannot be accessed outside

The most natural application of internal classes is to declare the classes that are only internally used in another class, or the declaration is closely related to another class. As shown in List B, it is a simple implementation of a linked list. Since the Node class is usually only used within the range of LinkedList, it is best to declare the Node as an internal class of LinkedList. Access control modifiers suitable for class members also apply to internal classes; that is, internal classes can have Package, Protected, Private, and Public access, their semantics and normal semantics have no difference. Since Node is to be used outside LinkedList, it declares it as public. However, the modifier Static has a different meaning. When applied to internal classes, it declares that the class is the same semantic as other classes, that is, instantiate, and use it like a standard class. The only difference is that it has full access to all static members of the external class. Listing C shows a simple program that creates a linked list and print it to the standard output device.

Non-static internal classes If the internal class does not specify a Static modifier, fully access all members of the external class, including instance fields and methods. To achieve this, a non-static internal class stores an implicit reference to an externally class. Therefore, instantiating a non-static internal class requires a new statement of different grammar: .new This form of New statement requires an instance of the external class, enabling the internal class to be created in the context of that instance. Note List A declare several non-static internal classes, and instantiate them in Method1 with standard New statements. The reason can be done, because Method1 is an example method of the external class, so the new statement is implicitly implicitly performed in the context of an instance of the external class. The modified syntax is required only when an unstatic internal class is instantially an unstatic internal class outside or in the context of other objects. However, non-static internal classes have some limits. In particular, they cannot declare static initialization lists and static members unless they are in a constant field. In addition, the internal declaration of the method is not accessible to the local variables and parameters of the method unless they are initialized into final. An anonymous anonymous class is a class that cannot be named, so there is no way to reference them. You must declare them as part of the New statement when you are created. This should be used in another form of new statement, as shown below: New This form of new statement declares a new anonymous class, which extends a given class, Or achieve a given interface. It also creates a new instance of that class and returns it as the result of the statement. The class to be extended and the interface to be implemented is the operand of the new statement, followed by the subject of anonymous class. If an anonymous class extends to another class, its body can access a member of the class, override it, etc., which is the same as any other standard class. If an anonymous class implements an interface, its body must implement the interface method. Note that the declaration of anonymous classes is performed at compile time, instantiation is performed at runtime. This means that a New statement in the FOR loop creates several instances of the same anonymous class, not an instance of several different anonymous classes. Technically, anonymous classes can be considered as non-static internal classes, so they have the same permissions and restrictions on the non-static internal class declared within the method. If the task to be executed requires an object, it is not worth creating a new object (why it might be too simple, or because it is only in one way), anonymous class is very useful. Anonymous classes are especially suitable for quick creation of event handles in a Swing application. Listing D is a very simple Swing application that shows several concepts related to anonymous classes. This example creates two anonymous classes. The first to extend Java.awt.Event.WindowAdapter and call the application's onclose method when the application window is turned off. Even if OnClose is declared as Private, anonymous class can call it because anonymous classes are essentially an internal class of the application class. The second anonymous class implements the java.awt.actionListener interface, which closes the application window after a button is pressed. Note Anonymous class can access the local variable Frame. This is due to the internal declaration of anonymous classes in the same way as Frame. However, Frame is to be declared as Final, otherwise the compilation error is generated.

More optimized interior and anonymous classes are the two excellent tools for Java 1.1 for us. They provide better packages, the result is to make the code more easily understand and maintain, so that the relevant classes can exist in the same source code file (this is due to internal classes), and avoiding a large number of programs Class (this is due to anonymous class).

转载请注明原文地址:https://www.9cbs.com/read-39541.html

New Post(0)