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 + " 第四" + i); } public void second(Value v, int i)
{
System.out.println(v.i + " 第一" + i);
i = 0;
v.i = 20;
System.out.println(v.i + "第二 " + i);
Value val = new Value();
v = val;
System.out.println(v.i + " 第三 " + i);
}
}结果是:为什么??
25 第一5
20第二 0
15 第三 0
20 第四5高人指点!
[email protected]
http://my.6cncn.cn
{
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 + " 第四" + i); } public void second(Value v, int i)
{
System.out.println(v.i + " 第一" + i);
i = 0;
v.i = 20;
System.out.println(v.i + "第二 " + i);
Value val = new Value();
v = val;
System.out.println(v.i + " 第三 " + i);
}
}结果是:为什么??
25 第一5
20第二 0
15 第三 0
20 第四5高人指点!
[email protected]
http://my.6cncn.cn
解决方案 »
- 用File创建了一个文件夹,如何用代码打开它?
- 在Java中用SQL语句删除access数据库的记录,为什么用access打开时记录还在?在线等!
- 当一个流做为参数传入另外一个流,需要关闭吗?例如本题的fo.close();
- 如何在一个JFrame中加上一个网页上连接??
- 从数据库中查询数据的效率问题!
- 为什么RandomAccessFile不能运行.
- 如何控制HttpUrlConnection的超时时间
- 我有线段 问题
- 怎样在Application程序中加音乐
- 求助——下载工具支持FTP或者ED2K等其他协议下载
- 如何用Process控制Process实例(exec方法返回的),能向Process进程中输入数据用getInputStream()
- 【求助】如何遍历一个二叉树!(老总快给我逼疯了,急啊)
-------------------前面一,二,三都没问题第四个的vi.i为什么是20
i的道理就很显然了,一定是5。
second返回以后,依旧返回作为参数的对象v,而这个对象v的i已经被更改为20;
i的道理就很显然了,一定是5。
--------------------------------------------------------------------------- second(v, i);
把v传进去了!可是当
System.out.println(v.i + "第二 " + i);
Value val = new Value();
v = val;//重新赋直了!难道外面的变量v 没有指到new Value();
--------------------------------------------
public void second(Value v, int i)传人的v和函数second的参数v是什么关系?
v是相当于在public void second里面的一个变量吗?
当调用方法second(v, i);的意思是
second(Value v=v, i);
------------------------------------------
v是一个变量可以指到其他地方吗?
如v = new Value();
当调用方法second(v, i);的意思是
second(Value v=v, int i=i);
------------------------------------------
v是一个局部(方法里面的)变量可以指到其他地方
如v = new Value();
Value val = new Value();
v = val;//是这句吧,其实这句可以理解成C中的指针,但是在这个second内有效方法,
System.out.println(v.i + " 第三 " + i);
PS:java对类的传递不同于C++,不是引用调用,而是值传递
顺便附上一段core java 的源代码,大家仔细体会
@version 1.00 2000-01-27
@author Cay Horstmann
*/public class ParamTest
{
public static void main(String[] args)
{
/*
Test 1: Methods can't modify numeric parameters
*/
System.out.println("Testing tripleValue:");
double percent = 10;
System.out.println("Before: percent=" + percent);
tripleValue(percent);
System.out.println("After: percent=" + percent); /*
Test 2: Methods can change the state of object
parameters
*/
System.out.println("\nTesting tripleSalary:");
Employee harry = new Employee("Harry", 50000);
System.out.println("Before: salary=" + harry.getSalary());
tripleSalary(harry);
System.out.println("After: salary=" + harry.getSalary()); /*
Test 3: Methods can't attach new objects to
object parameters
*/
System.out.println("\nTesting swap:");
Employee a = new Employee("Alice", 70000);
Employee b = new Employee("Bob", 60000);
System.out.println("Before: a=" + a.getName());
System.out.println("Before: b=" + b.getName());
swap(a, b);
System.out.println("After: a=" + a.getName());
System.out.println("After: b=" + b.getName());
} public static void tripleValue(double x) // doesn't work
{
x = 3 * x;
System.out.println("End of method: x=" + x);
} public static void tripleSalary(Employee x) // works
{
x.raiseSalary(200);
System.out.println("End of method: salary="
+ x.getSalary());
} public static void swap(Employee x, Employee y)
{
Employee temp = x;
x = y;
y = temp;
System.out.println("End of method: x=" + x.getName());
System.out.println("End of method: y=" + y.getName());
}
}class Employee // simplified Employee class
{
public Employee(String n, double s)
{
name = n;
salary = s;
} public String getName()
{
return name;
} public double getSalary()
{
return salary;
} public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
} private String name;
private double salary;
}
谁顶给谁分!
http://community.csdn.net/Expert/topic/5055/5055552.xml?temp=.322262