Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand reflection calling members

Shulou Source: shulou.com Published: 2022-06-03 05:49:56 11月10日 Update

This article mainly explains "how to understand reflection call members". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to understand reflection call members"!

reflection call member

The last core component of the class structure is the member (Field), which is called the member attribute in most cases. The acquisition of member information is also completed through the Class class. The following two groups of operation methods are provided in this class:

Get all members of this class:

public Field[] getDeclaredFields() throws SecurityException

Gets the specified member of this class:

public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException

Get all members of parent class: public Field[] getFields() throws SecurityException

Gets specified members of parent class: public Field getField(String name) throws NoSuchFieldException, SecurityException

Example: Modify parent class to operate on:

AbstractBase:

public abstract class AbstractBase {protected static final String BASE = "www.mldn.cn";private String info = "HELLO MLDN";public AbstractBase() {}public AbstractBase(String msg) {}}

IChannelService:

public interface IChannelService { public static final String NAME = "mldnjava"; public boolean connect();}

Example: Getting members of a class

import java.lang.reflect.Field;public class JavaAPIDemo {public static void main(String[] args) throws Exception { Class cls = Class.forName("cn.mldn.vo.Person"); { //Get public member information in parent class Field fields [] = cls.getFields(); //Get member for (Field fie : fields) { System.out.println(fie); } } System.out.println("-------------"); { //Get the members defined in the subclass Field fields [] = cls.getDeclaredFields();for (Field fie : fields){ System.out.println(fie); } } }}

Implementation results:

However, the most important form of operation in the Field class is not to get all the members, but the following three methods:

Set attribute content:

public void set(Object obj, Object value) throws IllegalArgumentException, IllegalAccessException

Get attribute content:

public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException

Unsealing:

public void setAccessible(boolean flag)

All members are space allocated after object instantiation, so you must instantiate the object before you can operate on members.

Example: Directly calling the name private member in the Person class

import java.lang.reflect.Field;public class JavaAPIDemo {public static void main(String[] args) throws Exception { Class cls = Class.forName("cn.mldn.vo.Person"); Object obj = cls.getConstructor().newInstance(); //Instantiate object (assign member space) FieldnameField = cls.getDeclaredField("name"); //Get member objectnameField.setAccessible(true); //Private attributes must be unencapsulated first, nameField.set(obj,"Tomato Strong"); //Equivalent to: Person object.name = "Tomato Strong"System.out.println(nameField.get(obj)); }}

Through a series of analysis, it can be found that the structure, method and member attribute in the class can be called through reflection, but the reflection of the member is rarely handled directly, most operations should be handled by setter and getter, so the above code can only be said to be reflective, but it does not have the actual ability to use, and for the Field class, only one method is most commonly used in actual development:

Get Member Type:

public Class getType()

Example: Get the type of name member in Person class

import java.lang.reflect.Field;public class JavaAPIDemo { public static void main(String[] args) throws Exception { Class cls = Class.forName("cn.mldn.vo.Person"); Field nameField = cls.getDeclaredField("name");System.out.println(nameField.getType().getName()); //Get full name, package. class java.lang.StringSystem.out.println(nameField.getType().getSimpleName()); //get class name String}}

When reflection processing is carried out in later development, the Field class and Method class are often used to implement the invocation of the setter method in the class.

At this point, I believe everyone has a deeper understanding of "how to understand reflection call members," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Tags: Members reflections methods objects properties content examples instances actual tomato processing information most of which are in most type allocation learning encapsulation development Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn Docker Shulou Tech Info NVidia