黑马程序员技术交流社区
标题:
final 反射问题 谁能解释一下
[打印本页]
作者:
lzw123451
时间:
2013-3-14 17:03
标题:
final 反射问题 谁能解释一下
本帖最后由 李志卫 于 2013-3-16 23:52 编辑
import java.lang.reflect.Field;
public class Test9{
public final int x = 100;
public int Method(Test9 t1,Test9 t2) throws Exception{
int i = t1.x;
System.out.println("i's value is "+i);
changeX(t1);
System.out.println("i2's value is "+t1.x);
int j = t2.x;
System.out.println("j's value is "+j);
return j - i;
}
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 100
j's value is 100
0
import java.lang.reflect.Field;
public class Test9{
public final int x;
public Test9()
{
x = 100;
}
public int Method(Test9 t1,Test9 t2) throws Exception{
int i = t1.x;
System.out.println("i's value is "+i);
changeX(t1);
System.out.println("i2's value is "+t1.x);
int j = t2.x;
System.out.println("j's value is "+j);
return j - i;
}
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
为什么两种情况不一样?
作者:
沈文杰
时间:
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