A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© kira 中级黑马   /  2014-2-24 15:55  /  827 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

需求是利用反射的思想 将字符串中的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
求助这是怎么回事 我该怎么改


评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1

查看全部评分

1 个回复

倒序浏览
好吧 我找到错误了 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. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马