写一个方法
public void setProperty(Object obj,String propertyName,Object value){}
此方法可以将obj对象中名为propertyName的属性值设置为value
public class Tool {
public void setProperty(Object obj, String propertyName, Object value) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Class c = obj.getClass();
Field field = c.getDeclaredField(propertyName);
field.setAccessible(true);
field.set(obj, value);
}
} |
|