Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What function does the java reflection mechanism provide

Shulou Source: shulou.com Published: 2022-05-31 10:48:34 09月24日 Update

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.

Tags: Method reflection run object private function property mechanism knowledge variable member industry process constructor different practical content dynamic instance utility Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Technology Xiaomi Shulou Information macOS