import java.lang.reflect.Field;
public class Test {
public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
Entity e = new Entity();
Field[] fields = e.getClass().getFields();
for(Field fiel : fields){
if(fiel.getType() == String.class){
String oldString =(String)fiel.get(e);
String newString = oldString.replace('a', 'b');
fiel.set(e, newString);
}
}
System.out.println(e);
}
}
class Entity{
private String a = "aaaa";
private String b = "aaaa";
private String c = "aaaa";
@Override
public String toString(){
return a+ b+c;
}
}
我的反射为什么运行不出来了?
|