Class 1:
public class Test {
public int i = 1;
public String s1 = "itcast";
public String s2 = "all";
private String s3 = "case";
@Override
public String toString() {
return s1 +" | "+ s2 +" | "+ s3;
}
}
Class 2:
public class FanSe {
public static void main(String[] args)throws Exception {
Class cls1 = Class.forName("cn.string_a_b.Test");
method(cls1);
}
public static void method(Object obj)throws Exception{
Field[] f1 = obj.getClass().getDeclaredFields();
System.out.println("f1"+ f1);
for (Field field : f1) {
Object f0 = field.getType();
if(f0 == String.class){
String oldValue = (String)field.get(obj);
String newValue = oldValue.replace('a', 'q');
field.set(obj, newValue);
}
}
}
}
代码就是上面这些了,已运行就报错,但是一直找不到错误的原因是什么,请哪位牛哥给分析一下,谢谢!(在线蹲着等{:soso_e123:}....)
|