//定义一个类,定义几个String 的成员变量
public class LeiDeBianLian {
private int y;
public int x;
String aa="sad";
String sd="sdadas";
String dsa="sadasd";
public LeiDeBianLian(int y, int x) {
super();
this.y = y;
this.x = x;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return aa+" : "+sd+" : "+dsa;
}
}
import java.lang.reflect.Field;
public class HuoQuBianLiang {
/**
* @param args
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
// TODO Auto-generated method stub
LeiDeBianLian l=new LeiDeBianLian(3,56);
Tong(l);
System.out.println(l);
}
private static void Tong(Object obj) throws IllegalArgumentException, IllegalAccessException
{
Field[] fields=obj.getClass().getFields();
for(Field field : fields)
{
if(field.getType()==String.class)
{
String oldStr=(String) field.get(obj);
String newStr=oldStr.replace('a', 'g');
field.set(obj, newStr);
}
// 这个例子的String的变量值没有被改变 ,
// 可能晕了,不知道怎么弄了
|