In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "what function does the java reflection mechanism provide". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what function the java reflection mechanism provides" can help you solve the problem.
Function description
1. Running to determine the class to which any object belongs.
2. Construct objects of any class at run time.
3. Judge the member variables and methods of any class in operation.
4. Get the information in the process of running.
The runtime calls member variables and methods of any object.
The runtime processes comments.
Generate a dynamic proxy.
Example
@ Testpublic void test1 () throws Exception {Class clazz = Person.class; / / 1. Through reflection, create the Person class object Constructor cons = clazz.getConstructor (String.class, int.class); Person person = cons.newInstance ("Tom", 12); System.out.println (person); / / Person {name='Tom', age=12} / / 2. Through reflection, call the property specified by the object, method / call property Field age= clazz.getDeclaredField ("age"); age.setAccessible (true); age.set (person, 10); System.out.println (person.toString ()); / / Person {name='Tom', age=10} / / call method Method show = clazz.getDeclaredMethod ("show"); show.invoke (person) / / my name is Tom and age is 10 System.out.println ("="); / / through reflection, you can call the private structure of the Person class. For example, private constructors, methods, properties / / calls private constructors Constructor cons1 = clazz.getDeclaredConstructor (String.class); cons1.setAccessible (true); Person p1 = cons1.newInstance ("Bruce"); System.out.println (p1); / / Person {name='Bruce', age=0} / / calls private properties Field name= clazz.getDeclaredField ("name"); name.setAccessible (true) Name.set (p1, "Jarry"); System.out.println (p1); / / call the private method Method nation = clazz.getDeclaredMethod ("nation", String.class); nation.setAccessible (true); Object nation1 = (String) nation.invoke (p1, "China"); / / equivalent to String nation = p1.showNation ("China") System.out.println (nation1) / / I come from China} this is the end of the introduction to "what does the java reflection mechanism provide?" Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.