黑马程序员技术交流社区

标题: 求教一个反射问题 大神速进 [打印本页]

作者: kira    时间: 2014-2-24 15:55
标题: 求教一个反射问题 大神速进
需求是利用反射的思想 将字符串中的b变成a
  1. import java.lang.reflect.Field;
  2. import java.lang.reflect.InvocationTargetException;



  3. public class ConDemo {


  4. public static void main(String[] args) throws Exception {
  5. // TODO Auto-generated method stub

  6. Reflectpo rp = new Reflectpo(3,5);


  7. changeStringFun(rp);
  8. System.out.println(rp);

  9. }

  10. private static void changeStringFun(Object o) throws Exception{//这里传入Object
  11.         //得到字段
  12.         Field[] f = o.getClass().getFields();
  13.         //对字段进行遍历
  14.         for(Field ff:f){
  15.                
  16.                 //判断是不是String
  17.                 if(ff.getType()==String.class){//字节码用等号比 得到字段
  18.                         String olds =  (String)ff.get(o);
  19.                         String news = olds.replace('b', 'a');
  20.                         //修改字段
  21.                         ff.set(olds, news);
  22.                 }
  23.                
  24.         }
  25.        
  26.    }

  27. }
  28. class Reflectpo{//创建一个类
  29. private int x,y;
  30. public String s1 ="ball";//新加两个成员变量
  31. public String s2 ="car";
  32. Reflectpo(int x ,int y){
  33. super();
  34. this.x=x;
  35. this.y=y;

  36.                 }
  37. public String toString(){
  38.        
  39.         return s1+"::"+s2;
  40.         }

  41. }
复制代码
我的代码 结果却发现即使覆盖了toString方法之后
还是 报了个异常
Exception in thread "main" java.lang.IllegalArgumentException: Can not set java.lang.String field com.XXX.Reflectpo.s1 to java.lang.String
求助这是怎么回事 我该怎么改



作者: kira    时间: 2014-2-24 16:05
好吧 我找到错误了 set方法传参错了
  1. import java.lang.reflect.Field;
  2. import java.lang.reflect.InvocationTargetException;



  3. public class ConDemo {


  4. public static void main(String[] args) throws Exception {
  5. // TODO Auto-generated method stub

  6. Reflectpo rp = new Reflectpo(3,5);


  7. changeStringFun(rp);
  8. System.out.println(rp);

  9. }

  10. private static void changeStringFun(Object o) throws Exception{//这里传入Object
  11.         //得到字段
  12.         Field[] f = o.getClass().getFields();
  13.         //对字段进行遍历
  14.         for(Field ff:f){
  15.                
  16.                 //判断是不是String
  17.                 if(ff.getType()==String.class){//字节码用等号比 得到字段
  18.                         String olds =  (String)ff.get(o);
  19.                         String news = olds.replace('b', 'a');
  20.                         //修改字段
  21.                         ff.set(o, news);
  22.                 }
  23.                
  24.         }
  25.        
  26.    }

  27. }
  28. class Reflectpo{//创建一个类
  29. private int x,y;
  30. public String s1 ="ball";//新加两个成员变量
  31. public String s2 ="car";
  32. Reflectpo(int x ,int y){
  33. super();
  34. this.x=x;
  35. this.y=y;

  36.                 }
  37. public String toString(){
  38.        
  39.         return s1+"::"+s2;
  40.         }

  41. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2