package com.heima.userJSTL;
import java.lang.reflect.Field;
public class GetFiledTheOne {
public static void main(String[] args) throws Exception {
/* //获取运行时类的指定属性并且进行复制和获取属性值的操作
//方法1(此方法只能获得被public修饰的属性,所以在实际开发中不太常用)
Class<Person> aClass = Person.class;
Field name = aClass.getField("name");//获得了当前运行时类的实例属性
//赋值
//因为是实例属性,所以我们需要提前穿件一个对象对这个对象进行该属性的赋值
//创建对象
Person person = aClass.newInstance();
//对person对象的name属性进行复制
name.set(person,"张三");//对person对象的name属性赋值张三
Object o = name.get(person);//获得person对象的name属性
System.out.println(o);*/
//方法2(在开发中我们一般使用方法2)
Class<Person> aClass = Person.class;
/* Field age = aClass.getDeclaredField("age");//age属性是被private修饰的
//创建当前运行时类的对象
Person person = aClass.newInstance();
age.setAccessible(true);//暴力反射,设置当前属性是可以被访问(赋值和修改)的
//对age进行赋值,但是赋值之前需要进行暴力反射操作
age.set(person, 23);
Object o = age.get(person);
System.out.println(o);*/
//获取类属性
Field aaa = aClass.getDeclaredField("aaa");
aaa.setAccessible(true);//暴力反射
aaa.set(null,23);
Object o1 = aaa.get(null);
System.out.println(o1);
}
}