class SetProperty{
public void setProperty(Object obj, String propertyName, Object value){
try {
Field f = obj.getClass().getDeclaredField(propertyName);
f.setAccessible(true);
f.set(obj,value);
} catch (Exception e) {
System.out.println("修改失败");
}
}
}
class Test{//测试
private String s = "true";
public static void main(String[] args) {
Test t = new Test();
SetProperty s = new SetProperty();
s.setProperty(t, "s","false");
System.out.println(t.s);
}
}
此代码能实现改正么,value可是object的,怎么能改成string呢?
|