package J20250806.reflection;

import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * @author WanJl
 * @version 1.0
 * @title Demo02_FieldTest
 * @description
 * @create 2025/8/6
 */
public class Demo02_FieldTest {
    public static void main(String[] args) throws ClassNotFoundException {
        Class<?> personClass = Class.forName("J20250806.reflection.Person");
        Field[] fields = personClass.getFields();
        System.out.println(Arrays.toString(fields));
        Field[] declaredFields = personClass.getDeclaredFields();
        System.out.println(Arrays.toString(declaredFields));
        String f="id";
        try {
            Field field = personClass.getField(f);
            System.out.println(field);
        } catch (NoSuchFieldException e) {
            System.out.println("没有名为的"+f+"公共成员变量");
        }
        f="name";
        try {
            Field field =personClass.getDeclaredField(f);
            System.out.println(field);
        } catch (NoSuchFieldException e) {
            System.out.println("没有名为"+f+"成员变量");
        }
    }
}
