In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "what is the use of the reflection mechanism in Java", the content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn this article "what is the use of reflection mechanism in Java".
Reflection:
You can dynamically get the members of the specified class and create class objects.
Advantage: the expansibility of the program is improved.
Zhang San, Li Si and other objects in life can be described in the form of Person class.
When class files are generated, these class files are also things in life, so these class files can also be described, and the corresponding type of description is Class.
In java, each bytecode file has a corresponding Class object. It includes not only reference data types, but also basic data types. Int.Class
String.class----Class Class class1=String.class; String s = "abc"; Class class2=s.getClass (); Class class3 = Class.forName ("java.lang.String")
These are the three ways to get a bytecode file.
In the past, the object was manipulated using the new keyword, and through the object. Make a call in a member way
Person p = new Person (); / / 1. Just load the Person.class file. / / 2. Initialize the object. P.function ("heihei"); / / when an object calls a member method, two factors need to be clear: 1. Object 2. Get parameter list
The above actions can now be done through bytecode objects.
/ / load the Person.class file and get the Class object Class clazz = Class.forName ("Person") of the Person.class file object; / / create and initialize the specified class through the bytecode file object. Person p = (Person) clazz.newInstance (); / / call member / / p.function ("heihei") through object
The class can be passed as a parameter, so can the method, because the method exists in the bytecode file, so you can get the contents of the bytecode file through the Class object.
Method m = Class.getMethod ("function", String.class); m.invoke (p, "heihei")
What are the benefits of passing classes or methods as parameters for development?
Requirements: design a motherboard, in order to improve the scalability in the later stage, that is, to improve the function of the computer in the later stage, the external interface of PCI is provided. To facilitate the expansion of computer functions.
Interface PCI {void open (); void close ();} class MainBoard {public void usePCI (PCI p) {p.open (); p.close () }} class MainBoardDemo {public static void main (String [] args) {MainBoard mb=new MainBoard (); / / mb.usePCI (null); mb.usePCI (NetCard) }}
In the later stage, in order to expand the computer function, we need to add a network card, only need to define a network card class to achieve the PCI interface, as long as override this rule, the motherboard can use the card.
Class NetCard implements PCI {public void open (); {system.out.println ("open");} public void close (); {system.out.println ("close") }}
Then in order to use its network card, another step is to establish the network card object in the defined application and pass it as a parameter, then it is to modify the original program. This is not conducive to the robustness of the program.
Can you run these later subclass objects without modifying the source code?
As long as in the early design, the later designated class for the establishment of the object, so that the later subclass object does not need to establish the object, as long as the subclass name can be told.
In order to get the late object, and can be used in the early stage, or provide a configuration file. The previous program can manipulate the configuration file directly, and the later subclass only needs to save the subclass name into the configuration file.
At this point, you need to dynamically get the specified class and create the object in advance.
The reflection mechanism is used. Modify the application again. Class MainBoardDemo {public static void main (String [] args) {MainBoard mb=new MainBoard (); File file=new File ("conf.txt"); BufferenReader buff = new BufferenReader (new FileReader (file)); String className = buff.readLine () Class clazz = Class.forName (className); PCI p = (PCI) clazz.newInstance (); mb.usePCI (p);}}
Profile conf.txt
When the network card or sound card appears later, you only need to save the full class name of the subclass into the configuration file, and the source program does not need to modify it.
In this case, you can see that reflection brings a lot of extensibility to our program.
The above is all the content of the article "what is the use of reflection in Java". 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.