public static void getFieldDemo()throws Exception{
String name="bean.Person";
Class clazz=Class.forName(name);
Field field=clazz.getDeclaredField("age");
Object obj=clazz.newInstance();
field.setAccessable(true);
field.set(obj,89);
Object o=field.get(obj);
System.out.println(o);
}
复制代码
Field field=clazz.getDeclaredField("age");异常信息下面:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Field cannot be resolved to a type