〖反射机制〗Field属性对象
获取和设置,对象的所有(public修饰的)属性值
package test;
import java.lang.reflect.Field;
public class Test {
public String a;
protected String b;
String c;
private String d;
public static void main(String[] args) throws Exception {
Test test = new Test();
/ 获取类对象
Class testClass = Class.forName("test.Test");
/ 根据类对象获取Field对象
Field a = testClass.getField("a");
/ 使用Field对象获取对象的(所有public修饰的)属性值
System.out.println(a.get(test));
System.out.println("----------------------------------------");
/ 使用Field对象给对象的属性赋值
a.set(test, "微微一笑很倾城");
System.out.println(test.a);
}
}
获取和设置,对象的所有属性值
我寻见一片海 碧蓝且耀着光
大片船只航行其上 都向着远方
Shared by Foriver_江河