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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 位雪 中级黑马   /  2012-10-6 02:17  /  1607 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 位丹丹 于 2012-10-6 20:13 编辑

高新技术反射中代码:
  1. package day01;

  2. public class ReflectPoint {
  3.           private int x;
  4.           public int y;
  5.           public String str1 = "Hello,my dear friends";
  6.           public String str2 = "I don't think you can do that";

  7.           public ReflectPoint(int x, int y) {
  8.                      super();
  9.                      this.x = x;
  10.                      this.y = y;
  11.          }

  12.             @Override
  13.           public String toString() {
  14.                      return "ReflectPoint [str1=" + str1 + ", str2=" + str2 + "]";
  15.           }

  16. }


复制代码
package day01;

import java.lang.reflect.Field;

public class Reflect {

              public static void main(String[] args) throws Exception {

                            ReflectPoint pt1 = new ReflectPoint(3,5);//pt1到低表示哪个对象?

                            Field fieldY = pt1.getClass().getField("y");//此处pt1表示什么内容
                            System.out.println(fieldY.get(pt1));

                           changeStringValue(pt1);//此处pt1表示什么内容?
                           System.out.println(pt1);
               }

               private static void changeStringValue(Object obj) throws Exception {
                            Field[] fields = obj.getClass().getFields();

                            for(Field field:fields)
                           {
                                     if(field.getType().equals(String.class))
                                     {
                                             String oldValue = (String)field.get(obj);
                                             String newValue = oldValue.replace('n', 'w');
                                             field.set(obj, newValue);
                                     }
                            }
               }
}
Field fieldY = pt1.getClass().getField("y");中的pt1与changeStringValue(pt1);中的pt1相同吗?很不理解,它们具体表示什么内容,望指点迷津

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 赞一个!

查看全部评分

5 个回复

倒序浏览
ReflectPoint pt1 = new ReflectPoint(3,5);  //这句话到底做了什么,一共 3 步 如下所示

(1)在栈中声明一个变量 pt1 这是一个ReflectPoint  类型的引用
(2)在堆中new 出一个ReflectPoint(3,5) 的对象
(3)让pt1  这个ReflectPoint   引用指向一个ReflectPoint 对象

so~pt1 是一个指向 ReflectPoint(3,5) 对象的引用 ,这个引用没有得到任何改变,所以两个地方的没有区别

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 赞一个!

查看全部评分

回复 使用道具 举报
本帖最后由 佘天宇 于 2012-10-6 10:18 编辑

           !!
回复 使用道具 举报
  1. package cn.itcast.heima.technology;

  2. import java.lang.reflect.Field;

  3. class ReflectPoint {

  4.         private int x;
  5.         public int y;
  6.         public String str1 = "Hello,my dear friends";
  7.         public String str2 = "I don't think you can do that";
  8.         public ReflectPoint(int x, int y) {
  9.                 super();
  10.                 this.x = x;
  11.      this.y = y;
  12.         }


  13.    @Override

  14.    public String toString() {

  15.    return "ReflectPoint [str1=" + str1 + ", str2=" + str2 + "]";

  16.    }
  17. }



  18. public class ForReflect {

  19.         /**
  20.          * @throws Exception
  21.          */
  22.         public static void main(String[] args) throws Exception {
  23.         ReflectPoint pt1 = new ReflectPoint(3,5);//pt1到低表示哪个对象?
  24.         //你上面不是定义了类ReflectPoint,并且提供了带参数的构造方法,
  25.         //这pt1不就是指向创建的对象new ReflectPoint(3,5)么

  26.         Field fieldY = pt1.getClass().getField("y");//此处pt1表示什么内容
  27.         //根据对象获得自己的Class对象,再根据Class对象获取指定名称的公公成员字段对象
  28.         
  29.         
  30.         System.out.println(fieldY.get(pt1));//获取字段在对象普通pt1中的值


  31.        changeStringValue(pt1);//此处pt1表示什么内容?
  32.        //调用下面你定义静态的功能函数,并将pt1作为参数传递过去,pt1指向对象new ReflectPoint(3,5),引用型变量啊
  33.       
  34.       
  35.        //输出对象,自动调用toString()方法
  36.        System.out.println(pt1);
  37.         }

  38.         private static void changeStringValue(Object obj) throws Exception {
  39.         Field[] fields = obj.getClass().getFields();//获取字段,明显字段可能不为一,所以返回的是数组

  40.         for(Field field:fields)//遍历数组
  41.        {       
  42.                 //获取字段的Class类对象,并判断是不是String.class类型的
  43.             if(field.getType().equals(String.class))
  44.             {
  45.            
  46.                      String oldValue = (String)field.get(obj);//获取字段在对象obj(就是传入的对象)中的值
  47.                      
  48.                      String newValue = oldValue.replace('n', 'w');//将'n'替换成'w'
  49.                      
  50.                      field.set(obj, newValue);//在指定的对象上设置字段的值。
  51.              }
  52.         }

  53.         }
  54.         /*Field fieldY = pt1.getClass().getField("y");中的pt1与changeStringValue(pt1);中的pt1相同吗?
  55.          * 很不理解,它们具体表示什么内容,望指点迷津
  56.          //很明显,是一样的,同样的都是对象new ReflectPoint(3,5)的引用型变量
  57.        
  58. */
  59. }
  60. //还有问题可以加个好友,哈哈!

复制代码

点评

这个理由无懈可击。。。。。  发表于 2012-10-6 16:15

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 25分够了。

查看全部评分

回复 使用道具 举报
{:soso_e183:}
回复 使用道具 举报
无懈可击 ??{:soso_e127:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马