public class SetProperty {
private int x;
public int y;
String str ="ssksshopsszxss";
String str1 ="ssdvbwswssyy";
public SetProperty(int x, int y) {
super();
this.x = x;
this.y = y;
}
@Override
public String toString() {
return str+" : "+str1;
}
}
import java.lang.reflect.Field;
class TestReplace {// 测试
private String s = "true";// 这里同包所以用private测试可通过,property更没问题了吧
public static void main(String[] args) throws Exception {
SetProperty s = new SetProperty(2, 5);
Field fs = s.getClass().getField("y");
System.out.println(fs.get(s));
Field f = s.getClass().getDeclaredField("x");
f.setAccessible(true);
System.out.println(f.get(s));
chang(s);
System.out.println(s);
}
private static void chang(Object obj) throws Exception {
Field[] f = obj.getClass().getFields();
for (Field field : f) {
// 字节码比较用等号,不是字节码才用equals()
if (fi.getType() == String.class) {
String value1 = (String) field .get(obj);
String newvalue = value1.replace('s', 'a');//--------问题1
System.out.println("---" + newvalue);
field .set(obj, newvalue);
}
}
}
} 作者: 潘东升 时间: 2012-6-17 22:07
new value--中间是不是带了空格?作者: 郑传庆 时间: 2012-6-17 22:27