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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黑马张伟 黑马帝   /  2011-12-26 19:11  /  1443 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 黑马张伟 于 2011-12-27 08:42 编辑

package reflect;

import java.lang.reflect.Field;

public class ReflectTest {
        public static void main(String[] args) throws Exception {
                ReflectPoint pt1=new ReflectPoint();
                changeStringValue(pt1);
                System.out.println(pt1);
        }
        private static void changeStringValue(Object obj) throws Exception{
                Field [] fileds=obj.getClass().getFields();
                for(Field filed :fileds){
                        if(filed.getType()==String.class){
                                String oldValue =(String)filed.get(obj);
                                String newValue =oldValue.replace('b','a');
                                filed.set(obj,newValue);
                        }
                }
               
        }
}
另一个class
package reflect;

public class ReflectPoint {
      
        public String str1 ="sdfasdfsdfasds";
    public String str2 = "sdfasdgassdg";
    public String str3 ="vd;foijdf";
      
public ReflectPoint() {
           super();
           this.str1 = str1;
           this.str2 = str2;
           }
public String toString(){
         return str1+":"+str2+":"+str3;
}
      
}
没有调用成功ReflectPoint pt1=new ReflectPoint();这段代码有问题不知如何解决

评分

参与人数 1技术分 +1 收起 理由
王德云 + 1

查看全部评分

3 个回复

倒序浏览
代码没错 你str1 str2 str3 里面有b么?我怎么没看见啊!
回复 使用道具 举报
楼主代码毫无问题:

看下面代码:
  1. package reflect;

  2. public class ReflectPoint {

  3.         public String str1 = "ab1";
  4.         public String str2 = "ab2";
  5.         public String str3 = "ab3";

  6.         public String toString() {
  7.                 return str1 + ":" + str2 + ":" + str3;
  8.         }

  9. }

  10. package reflect;

  11. import java.lang.reflect.Field;

  12. public class ReflectTest {
  13.         public static void main(String[] args) throws Exception {
  14.                 ReflectPoint pt1 = new ReflectPoint();
  15.                 changeStringValue(pt1);
  16.                 System.out.println(pt1);
  17.         }

  18.         private static void changeStringValue(Object obj) throws Exception {
  19.                 Field[] fileds = obj.getClass().getFields();
  20.                 for (Field filed : fileds) {
  21.                         if (filed.getType() == String.class) {
  22.                                 String oldValue = (String) filed.get(obj);
  23.                                 String newValue = oldValue.replace('b', 'a');
  24.                                 filed.set(obj, newValue);
  25.                         }
  26.                 }

  27.         }
  28. }

复制代码
输出:aa1:aa2:aa3

下面代码在程序当中是多余的:
  1. public ReflectPoint() {
  2.             super();
  3.             this.str1 = str1;
  4.             this.str2 = str2;
  5.             }
复制代码
回复 使用道具 举报
public ReflectPoint() {
            super();
            this.str1 = str1;
            this.str2 = str2;
            }
这段代码是错误的,你都没传参数str1和str2,怎么给this.str1和str2赋值呢,像上面所说,直接去掉代码就会有默认的构造方法,也是可以的。要不ReflectPoint pt1=new ReflectPoint()是不行的。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马