p { margin-bottom: 0.21cm; }
取得某一个变量的 class 变量名.getType()
得到一个成员变量 Field.get( 对象);
如果给一个成员赋值 Field.set( 对象, 值);
下面的实例是将一个对象中的字符串变量中的b 换成a
public static void changStringValue(Object obj){
try {
Field[] fields = obj.getClass().getFields();
for (Field field : fields){
if (field.getType() == String. class ){
String oldValue = (String)field.get(obj);
String newValue = oldValue.replace( 'b' , 'a' );
field.set(obj, newValue);
}
}
} catch (Exception e) {
// TODO : handle exception
}
}