注:本题目中有两处错误!
public class Test1 {
String s="basketbaoo";
String s2="balsl";
String s1="itcast";
@Override
public String toString() {
return "Test1 [s=" + s + ", s2=" + s2 + ", s1=" + s1 + "]";
}
}
public class Test2 {
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException
{
Test1 test=new Test1();
changeToString(test);
System.out.println(test);
}
public static void changeToString(Object obj) throws IllegalArgumentException, IllegalAccessException
{
Field[]fields=Object.class.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);
}
}
}
}
|