| 12345678910111213141516171819202122232425 | package J20250806.reflection;import java.lang.reflect.Field;/** * @author WanJl * @version 1.0 * @title Demo05_FieldTest * @description 获取成员变量的类型和访问修饰符 * @create 2025/8/6 */public class Demo05_FieldTest {    public static void main(String[] args) throws NoSuchFieldException {        Class<Person> personClass = Person.class;        Field nameField = personClass.getDeclaredField("name");        //暴力反射        nameField.setAccessible(true);        //获取成员变量的类型        Class<?> type = nameField.getType();        System.out.println(type);        //获取成员变量的修饰符        int modifiers = nameField.getModifiers();        System.out.println(modifiers);  //2 表示为私有的private修饰符    }}
 |