Home > Java > javaTutorial > body text

How to get object properties and values ​​using java reflection

小老鼠
Release: 2024-01-03 14:43:16
Original
1253 people have browsed it

Acquisition method: 1. Create a Person class and obtain the Class object of the class through reflection; 2. Use the getDeclaredFields method to obtain all fields of the class; 3. By traversing the field array, set the field to be Access the status, then use the get method to get the value of the field, and print the field name and value.

How to get object properties and values ​​using java reflection

Operating system for this tutorial: Windows 10 system, Dell G3 computer.

In Java, reflection is a mechanism that allows you to obtain class information, call class methods, and manipulate class properties at runtime. Using reflection, you can get the properties and values ​​of an object. The following is a simple example that demonstrates how to use Java reflection to obtain the properties and values ​​of an object:

import java.lang.reflect.Field;
public class ReflectExample {
    public static void main(String[] args) throws IllegalAccessException {
        // 创建一个示例对象
        Person person = new Person("John", 25, "123 Main St");
        // 获取Class对象
        Class<?> clazz = person.getClass();
        // 获取类的所有字段(包括私有字段)
        Field[] fields = clazz.getDeclaredFields();
        // 遍历字段数组
        for (Field field : fields) {
            // 设置字段为可访问,即使是私有字段也可以访问
            field.setAccessible(true);
            // 获取字段的名称
            String fieldName = field.getName();
            // 获取字段的值
            Object fieldValue = field.get(person);
            // 打印字段名和值
            System.out.println(fieldName + ": " + fieldValue);
        }
    }
}
// 示例类
class Person {
    private String name;
    private int age;
    private String address;
    public Person(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }
}
Copy after login

In the above example, we first created a Person class, and then obtained the Class object of the class through reflection. Next, use the getDeclaredFields method to obtain all fields of the class. By traversing the field array, we set the field to be accessible, then use the get method to obtain the value of the field, and print the field name and value.

It should be noted that the private fields are accessed through reflection, so when using reflection, you need to pay attention to the access permissions to the fields. In production code, reflection should be used with caution as it can break encapsulation and make code more difficult to understand and maintain.

The above is the detailed content of How to get object properties and values ​​using java reflection. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!