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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lzw123451 中级黑马   /  2013-3-14 17:03  /  1379 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 李志卫 于 2013-3-16 23:52 编辑
  1. import java.lang.reflect.Field;

  2. public class Test9{

  3. public final int x = 100;

  4. public int Method(Test9 t1,Test9 t2) throws Exception{

  5. int i = t1.x;

  6. System.out.println("i's value is "+i);

  7. changeX(t1);
  8. System.out.println("i2's value is "+t1.x);

  9. int j = t2.x;

  10. System.out.println("j's value is "+j);

  11. return j - i;

  12. }

  13. public static void changeX(Test9 t1) throws Exception{

  14. Class clazz = t1.getClass();

  15. Field fieldX = clazz.getDeclaredField("x");

  16. fieldX.setAccessible(true);

  17. fieldX.setInt(t1, 300);

  18. System.out.println("fieldX's vlaue is "+fieldX.getInt(t1));

  19. }

  20. public int test() throws Exception{

  21. return Method(this,this);

  22. }

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

  24. Test9 t1 = new Test9();

  25. System.out.println(t1.test());

  26. }

  27. }
复制代码
结果是
i's value is 100
fieldX's vlaue is 300
i2's value is 100
j's value is 100
0
  1. import java.lang.reflect.Field;

  2. public class Test9{

  3. public final int x;
  4. public Test9()
  5. {
  6. x = 100;
  7. }

  8. public int Method(Test9 t1,Test9 t2) throws Exception{

  9. int i = t1.x;

  10. System.out.println("i's value is "+i);

  11. changeX(t1);
  12. System.out.println("i2's value is "+t1.x);

  13. int j = t2.x;

  14. System.out.println("j's value is "+j);

  15. return j - i;

  16. }

  17. public static void changeX(Test9 t1) throws Exception{

  18. Class clazz = t1.getClass();

  19. Field fieldX = clazz.getDeclaredField("x");

  20. fieldX.setAccessible(true);

  21. fieldX.setInt(t1, 300);

  22. System.out.println("fieldX's vlaue is "+fieldX.getInt(t1));

  23. }

  24. public int test() throws Exception{

  25. return Method(this,this);

  26. }

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

  28. Test9 t1 = new Test9();

  29. System.out.println(t1.test());



  30. }

  31. }


复制代码
结果是
i's value is 100
fieldX's vlaue is 300
i2's value is 300
j's value is 300
200


为什么两种情况不一样?

点评

如果问题已经解决了,请将分类改为已解决,谢谢  发表于 2013-3-15 08:24

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

5 个回复

倒序浏览
难道是用记事本打的?头都看昏了,麻烦你排下版。。。这太不规范了。
回复 使用道具 举报
import java.lang.reflect.*;
public class Test9{

public final int x;

public Test9()

{

x = 100;

}
//输出两对象相减的结果

public int Method(Test9 t1,Test9 t2) throws Exception{

int i = t1.x;
//此时i为100
System.out.println("i's value is "+i);

changeX(t1); //调用方法后t1.x为300

System.out.println("i2's value is "+t1.x);

int j = t2.x;//j为300,this代表t1

System.out.println("j's value is "+j);

return j - i; //300-100

}
//更改对象属性值
public static void changeX(Test9 t1) throws Exception{

Class clazz = t1.getClass();

Field fieldX = clazz.getDeclaredField("x");

fieldX.setAccessible(true);

fieldX.setInt(t1, 300);

System.out.println("fieldX's vlaue is "+fieldX.getInt(t1));

}

public int test() throws Exception{

return Method(this,this);

}

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

Test9 t1 = new Test9();

System.out.println(t1.test());

}

}


/*运行结果是:

i's value is 100

fieldX's vlaue is 300

i2's value is 300

j's value is 300

200*/
//this代表同一对象

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

回复 使用道具 举报
参照这思路解决第二个!
回复 使用道具 举报
tianyun 发表于 2013-3-14 18:09
参照这思路解决第二个!

????
第一种情况 用反射最终没改变x的值
第二种情况 却改变成功了
为什么
回复 使用道具 举报
{:soso_e127:}还没人回答问题呢。上面的答案莫名奇妙,乱写一通
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马