import java.lang.reflect.*;
class MainDemo
{
public static void main(String[] args)throws Exception {
ReflecPoint rfp=new ReflecPoint(3,5);
changeStringValue(rfp);
System.out.println(rfp);
}
public static void changeStringValue(Object obj)throws Exception
{
Field[] files=obj.getClass().getFields();//获取所有成员变量
for(Field fi: files)
{
if(fi.getClass()==String.class)
{
String oldString=(String)fi.get(obj);
String newString=oldString.replace('b', 'a');
fi.set(obj, newString);
}
class ReflecPoint
{
private int x;
public int y;
public String str1="ball";
public String str2="basketball";
public String str3="itcast";
public ReflecPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
@Override
public String toString()
{
return str1+":"+str2+":"+str3;
}
}
我想要把字符串里的b换成a,结果最后的输出结果没有改变
|
|