package Fanshe;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class Fanshedeom3 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> clss = Class.forName("Fanshe.Student");
//通过成员变量设置值
//获取所有成员变量 遍历一遍
Field[] declaredFields = clss.getDeclaredFields();
for (Field a : declaredFields) {
System.out.println(a);
}
//获得指定成员变量
Field namefield = clss.getDeclaredField("name");
//无参构造
Constructor<?> stu = clss.getConstructor();
Object obj = stu.newInstance();
//设置stu的name值 这里也就是namefield的值
namefield.set(obj,"李丹"); //这里的意思是对obj对象的namefield的属性设置值为“李丹”;
System.out.println(obj);
}
}