本帖最后由 黑马张伟 于 2011-12-27 08:42 编辑
package reflect;
import java.lang.reflect.Field;
public class ReflectTest {
public static void main(String[] args) throws Exception {
ReflectPoint pt1=new ReflectPoint();
changeStringValue(pt1);
System.out.println(pt1);
}
private static void changeStringValue(Object obj) throws Exception{
Field [] fileds=obj.getClass().getFields();
for(Field filed :fileds){
if(filed.getType()==String.class){
String oldValue =(String)filed.get(obj);
String newValue =oldValue.replace('b','a');
filed.set(obj,newValue);
}
}
}
}
另一个class
package reflect;
public class ReflectPoint {
public String str1 ="sdfasdfsdfasds";
public String str2 = "sdfasdgassdg";
public String str3 ="vd;foijdf";
public ReflectPoint() {
super();
this.str1 = str1;
this.str2 = str2;
}
public String toString(){
return str1+":"+str2+":"+str3;
}
}
没有调用成功ReflectPoint pt1=new ReflectPoint();这段代码有问题不知如何解决 |