本帖最后由 左华清 于 2012-2-25 11:11 编辑
package cn.itcast.day01;
import java.lang.reflect.Field;
public class TestRefelect {
public static void main(String[]args)throws Exception{
RefelectPointDemo rpd=new RefelectPointDemo(3,5);
changValue(rpd);
}
public static void changValue(Object obj)throws Exception{
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);
}
}
}
}
package cn.itcast.day01;
public class RefelectPointDemo {
private int x;
public int y;
public String str1="ball";
public String str2="basketball";
public String str3="bank";
public RefelectPointDemo(int x, int y) {
super();
this.x = x;
this.y = y;
}
@Override
public String toString(){
return str1 + ":" + str2 + ":" + str3;
}
}
大家帮忙看看这段代码那有问题呀,不能输出. |