package my;
public class ReflectDemo
{
public String s1="abs";
private String s2="abstract";
@Override
public String toString()
{
return "ReflectDemo [s1=" + s1 + ", s2=" + s2 + "]";
}
}
package my;
import java.lang.reflect.Field;
public class ReflectTest_1 {
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException
{
ReflectDemo rd=new ReflectDemo();
Field[] fields=rd.getClass().getDeclaredFields();
for (Field field : fields)
{
if(field.getType()==String.class)
{
field.setAccessible(true);
String old=(String) field.get(rd);
String now=old.replace('b', 'a');
field.set(rd, now);
}
}
System.out.println(rd);
}
}
|