import java.lang.reflect.Field;
/**
* @author 万星明
* @version 创建时间:2018年10月26日 上午10:43:23
*/
public class 反射属性调用 {
public static void main(String[] args) throws Exception {
//创建反射对象
Class clazz = Class.forName("反射与URL.Student");
//通过反射对象创建实例对象
Object ob1 = clazz.newInstance();
//获取类私有的属性
Field ageField = clazz.getDeclaredField("age");
//设置属性的访问权限
ageField.setAccessible(true);
//给私有的或者公有的属性赋值
ageField.set(ob1, 11);
System.out.println(ageField.get(ob1));
}
}
反射:通过反射调用属性
最新推荐文章于 2024-03-31 02:30:53 发布