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);
}
}
}
}
张老师的视频中特意强调红色字体的if中的条件用“==”连接,为什么“==”比equals好呢? |