public class TestTemp {
public static void main(String[] args)throws Exception{
testE r = new testE(1);
System.out.println(r.a);//1
procee(r);
System.out.println(r.a);//还是1,为什么不是20?
}
public static void procee(testE o)
{
testE te = new testE(1);
te.a=20;
o=te;
System.out.println("~~~"+o.a);//20
}
}class testE
{
int a;
public testE(int a)
{
this.a=a;
}
}为什么不是20啊,o=te对象已经转过了啊,我传进去是个对象啊,为什么还是1?
//这里只是临时将o指向te,并没有改变原来的r的任何属性
o = te;
现在是 r和o对指向你在main里面创建的对象里
然后 procee方法里,你又把 o指向了另外一个 对象上,但是这个时候r还是指向原来的对象,r并没有变
变的只是o这个引用的指向 ,所以 还是1
testE te = new testE(1);
te又是一个新对象了。和第一个做参数传入的对象是两个对象。
最后他指向 te
但是主函数中的仍然指向t
public class TestTemp {
public static void main(String[] args)throws Exception{
testE r = new testE(1);
System.out.println(r.a);//1
r=procee(r);
System.out.println(r.a);//就是20了}
public static testE procee(testE o)
{
testE te = new testE(1);
te.a=20;
o=te;
System.out.println("~~~"+o.a);//20
return o;
}
}class testE
{
int a;
public testE(int a)
{
this.a=a;
}
}
2。一个方法可以可变一个对象参数的状态。
3。一个方法不能让对象参数引用一个新的对象。
testE r = new testE(1);
System.out.println(r.a);//1
procee(r);
System.out.println(r.a);//还是1,为什么不是20? } public static void procee(testE o) {
// testE te = new testE(1);
// te.a = 20;
// o = te;
o.a = 20;
System.out.println("~~~" + o.a);//20
}
}class testE {
int a; public testE(int a) {
this.a = a;
}
}
这样就可以变成20了,同意10楼的说法