In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use internal and anonymous classes to optimize Java code, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
Java 1.1 significantly simplifies the implementation of some practical structures by modifying the Java language specification. Among those changes, the most striking are inner classes and anonymous classes. If used properly, they can make programs easier to understand and maintain. Let's take a look at exactly how these features work, how to use them correctly, and how to avoid some common errors.
Inner class
In a nutshell, an "inner class" is a class declared inside another class. Starting with Java 1.1, you can declare one class in another, much like declaring fields and methods. A class that wraps an inner class declaration is called an "outer class".
In fact, the Java language specification allows you to do more things, including:
Declare a class in another class or an interface.
Declare an interface in another interface or in a class.
Declare a class in a method.
Class and interface declarations can be nested at any depth.
Listing An is a blank declaration of classes and interfaces that demonstrates these possibilities.
Using an import statement, you can omit the package name as you would any other standard class. In addition, in external classes, all inner classes and interfaces can be referenced with simple names (see the new statement in listing A). Note that you still need to specify Interface1 to reference Inner2 from Method1, because Inner2 is at a different level.
Table A summarizes the fully qualified names of each inner class and interface declared in listing A. After using the import statement, you can take a shorter form. Of course, in an external class, you can also omit the name 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 to Method1, so it is not accessible outside the method
Reference inner class
One of the most natural applications of inner classes is to declare classes that are only used within another class, or to declare classes that are closely related to another class. As shown in listing B, it is a simple implementation of a linked list. Since the Node class is usually used only within the scope of LinkedList, it is best to declare Node as an inner class of LinkedList.
The access control modifiers that apply to class members also apply to inner classes; that is, inner classes can have package, protected, private, and public access, and their semantics are no different from normal semantics. Because Node is to be used outside of LinkedList, declare it as public.
However, the modifier static has a different meaning. When applied to an inner class, the class it declares has the same semantics as other classes, that is, it can be instantiated and used 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 prints it to a standard output device.
Non-static inner class
If the inner class does not specify the static modifier, it has full access to all members of the external class, including instance fields and methods. To implement this behavior, the non-static inner class stores an implicit reference to an instance of the external class.
Therefore, instantiating a non-static inner class requires new statements with different syntax:
.new
This form of new statement requires an instance of the outer class so that the inner class can be created in the context of that instance. Notice that listing A declares several non-static inner classes and instantiates them in Method1 with standard new statements.
You can do that because Method1 is an instance method of an external class, so the new statement is executed implicitly in the context of an instance of the external class. The modified syntax is needed only if a non-static inner class is instantiated outside the external class or in the context of other objects.
However, non-static inner classes have some limitations. In particular, they cannot declare static initialization lists and static members unless they are in constant fields. In addition, inner classes declared within a method cannot access the method's local variables and parameters unless they are initialized to final.
Anonymous class
Anonymous classes are classes that cannot have names, so there is no way to reference them. They must be declared as part of the new statement at creation time.
This takes a different form of new statement, as follows:
New
This form of new statement declares a new anonymous class that extends a given class or implements 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 implement are the operands of the new statement, followed by the body of the anonymous class.
If an anonymous class extends another class, its principal can access the members of the class, override its methods, and so on, just like any other standard class. If an anonymous class implements an interface, its principal must implement the method of the interface.
Note that anonymous classes are declared at compile time and instantiated at run time. This means that a new statement in the for loop creates several instances of the same anonymous class, rather than one instance of several different anonymous classes.
Technically, anonymous classes can be considered non-static inner classes, so they have the same permissions and restrictions as non-static inner classes declared within methods.
Anonymous classes are useful if the task you are performing requires an object, but it is not worth creating an entirely new object (either because the required class is too simple, or because it is used only within a method). Anonymous classes are particularly useful for quickly creating event handlers in Swing applications.
Listing D is a very simple Swing application that shows several concepts related to anonymous classes. This example creates two anonymous classes. The first extends java.awt.event.WindowAdapter and calls the application's onClose method when the application window closes.
An anonymous class can call onClose even if it is declared as private, because an anonymous class is essentially an inner class of an application class. The second anonymous class implements the java.awt.ActionListener interface, which closes the application window after a button is pressed. Note that anonymous classes can access the local variable frame. This is because anonymous classes are declared inside the same method as frame. However, frame must be declared as final, otherwise a compilation error will be generated.
More optimized code
Internal and anonymous classes are two excellent tools that Java 1.1 provides for us. They provide better encapsulation, and the result is to make the code easier to understand and maintain, so that related classes can all exist in the same source code file (thanks to internal classes). And can avoid a program from producing a large number of very small classes (thanks to anonymous classes).
The above is all the content of the article "how to optimize Java code using internal and anonymous classes". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.