本帖最后由 宿万涛 于 2011-11-23 11:30 编辑
import java.lang.reflect.Field;
public class ReflectTest {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ReflectPoint pt1 = new ReflectPoint(3,5);
changeStringValue(pt1);
System.out.println(pt1);
}
private static void changeStringValue(Object obj) throws Exception {
// TODO Auto-generated method stub
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);
}
}
}
}
public class ReflectPoint {
private int x;
public int y;
String str1 = "abcde";
String str2 = "basketball";
String str3 = "itcast";
public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
@override
public String toString(){
return str1+":"+str2+":"+str3;
}
}
如题,我在运行这个程序,字符并没用改过来,我debug了一下,if之间的迭代语句没用执行,童鞋们给看看咋回事? |
|