黑马程序员技术交流社区

标题: final 反射问题 谁能解释一下 [打印本页]

作者: lzw123451    时间: 2013-3-14 17:03
标题: final 反射问题 谁能解释一下
本帖最后由 李志卫 于 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-14 17:35
难道是用记事本打的?头都看昏了,麻烦你排下版。。。这太不规范了。
作者: tianyun    时间: 2013-3-14 18:08
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代表同一对象

作者: tianyun    时间: 2013-3-14 18:09
参照这思路解决第二个!
作者: lzw123451    时间: 2013-3-14 18:22
tianyun 发表于 2013-3-14 18:09
参照这思路解决第二个!

????
第一种情况 用反射最终没改变x的值
第二种情况 却改变成功了
为什么
作者: lzw123451    时间: 2013-3-15 12:38
{:soso_e127:}还没人回答问题呢。上面的答案莫名奇妙,乱写一通




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2