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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王峰230321 中级黑马   /  2013-7-28 17:51  /  1488 人查看  /  12 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 杨兴庭 于 2013-7-28 23:21 编辑

  import java.lang.reflect.Field;class Preson
{
        String s1="abc";
        String s2="acb";
        String s3="bda";
        public String toString()
        {
                return s1+" "+s2+" "+s3;
        }
}

public class Fanshe
{   
         public static void show(Object obj)throws Exception
        {
                Field[] f=obj.getClass().getFields();
                for(Field f1:f)
                {
                        if(f1.getType()==String.class)
                        {
                                String oldV =(String)f1.get(obj);
                                String newV=oldV.replace('w', 'a');
                f1.set(obj,newV);
                        }
                }
        }
        public static void main(String...args)throws Exception
        {
                Preson p=new Preson();
                show(p);
                System.out.println(p);
        }
}

------------------------------------------------------------------------
反不了啊 反不了 .  愁人啊 愁人
给7L的截图..   第一张是new对象.  第二张是syso


222.jpg (25.81 KB, 下载次数: 0)

222.jpg

333..jpg (32.66 KB, 下载次数: 0)

333..jpg

评分

参与人数 1技术分 +1 收起 理由
杨兴庭 + 1

查看全部评分

12 个回复

倒序浏览
Field[] f=p.getClass().getFields();  你这个用的p.getClass
下面却用的obj
回复 使用道具 举报
本帖最后由 王峰230321 于 2013-7-28 18:22 编辑
tpsu88 发表于 2013-7-28 18:03
Field[] f=p.getClass().getFields();  你这个用的p.getClass
下面却用的obj

我用P也不行啊.  字符改不掉啊.  跟原来的一样..   我把编码该的跟老师一个模式也不行啊... 就是这样
-------------------------------------------------------------------------
public static void main(String[] args)throws Exception
        {
                Preson p=new Preson();
                show(p);
                System.out.println(p);
               
     }
        public static void show(Object obj)throws Exception
        {
                Field[] f=obj.getClass().getFields();
                for(Field f1:f)
                {
                        if(f1.getType()==String.class)
                        {
                                String oldV =(String)f1.get(obj);
                                String newV=oldV.replace('w', 'a');
                                f1.set(newV,oldV);
                        }
                }
               
        }
}
回复 使用道具 举报
f1.set(obj,newV); 你再试试
回复 使用道具 举报
tpsu88 发表于 2013-7-28 18:31
f1.set(obj,newV); 你再试试

不行昂- - . 还是 换不了.
回复 使用道具 举报
System.out.println(p);这句也跟老师的一样吗?
回复 使用道具 举报
修改的是成员  但是打印的是对象。。楼主又调皮了
回复 使用道具 举报
冒烟的芒果 发表于 2013-7-28 19:08
修改的是成员  但是打印的是对象。。楼主又调皮了

那应该打印什么呢. ?  把成员修改了.  类内部不就已经变了吗.  然后 new一个对象出来 不就应该是修改完成之后的结果吗. .    .     
我想错了吗. ?  不然  应该打印什么呢. ?
回复 使用道具 举报
冒烟的芒果 发表于 2013-7-28 19:06
System.out.println(p);这句也跟老师的一样吗?

求解.   有图. . .   
回复 使用道具 举报
冒烟的芒果 发表于 2013-7-28 19:06
System.out.println(p);这句也跟老师的一样吗?

求解. ! 有图
回复 使用道具 举报
  1. package Test;

  2. import java.lang.reflect.Field;

  3. class Preson {
  4.         public String s1 = "abc";
  5.         public String s2 = "acb";
  6.         public String s3 = "bda";

  7.         public String toString() {
  8.                 return s1 + " " + s2 + " " + s3;
  9.         }
  10. }

  11. public class Fanshe {
  12.         public static void show(Object obj) throws Exception {
  13.                 Field[] f = obj.getClass().getFields();
  14.                 for (Field f1 : f) {
  15.                         if (f1.getType() == String.class) {
  16.                                 String oldV = (String) f1.get(obj);
  17.                                 String newV = oldV.replace('a', 'w');  //这个地方注意
  18.                                 f1.set(obj, newV);
  19.                         }
  20.                 }
  21.         }

  22.         public static void main(String... args) throws Exception {
  23.                 Preson p = new Preson();
  24.                 show(p);
  25.                 System.out.println(p);
  26.         }
  27. }
复制代码

QQ图片20130728205727.jpg (5.66 KB, 下载次数: 0)

QQ图片20130728205727.jpg

评分

参与人数 1技术分 +1 收起 理由
杨兴庭 + 1

查看全部评分

回复 使用道具 举报
replace(char oldChar, char newChar)
回复 使用道具 举报

是因为 . public

- - 谢谢了.  
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马