大家来看看这道题的结果
class Value
{
public int i = 15;
}
public class Test
{
public static void main(String argv[])
{
Test t = new Test();
t.first();
}
public void first()
{
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i);
}
public void second(Value v, int i)
{
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i + " " + i);
}
}
帮我看看,这是为什么???
------解决方案--------------------class Value
{
public int i = 15;
}
public class Test
{
public static void main(String argv[])
{
Test t = new Test();
t.first();
}
public void first()
{
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i); //这里的i是5
System.out.println(v.i); //v.i由于被second修改为20 所以应该是20
}
public void second(Value v, int i)
{
i = 0;
v.i = 20; //这里对上边方法里的v.i重新复职为 20
Value val = new Value();
v = val; //改变本方法里的v的指向
System.out.println(v.i + " " + i); //v.i=15 因为是新声明的对象初始为15,i在上边给值是0
}
}
输出结果应该是 15,0,20吧?这题不太绕属于基础了