方法参数的值调用+引用调用

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

##【0】README

0.1)本文描述+源代码均 转自 corejavavolume 1, 旨在理清值调用+引用调用;

##【1】参数传递给方法的专业术语:

1.1)值调用:它表示方法接收的是调用者提供的值;

1.2)引用调用:它表示方法接收的是调用者提供的变量地址;

##【2】看个荔枝:

2.1)设一个方法视图将一个参数值增大3倍:

public static void tripleValue(double x)

{

x = 3 * x;

}

2.2)然后调用这个方法:

double percent = 10;

tripleValue(percent);

2.3)无论如何,调用这个方法后,percent的值还是10,下面看一下具体执行过程:

step1)x 被初始化percent值的一个copy;step2)x 被乘以 3 后等于 30, 但是 percent 仍然是 10;step3)这个方法结束后, 参数变量x 不在使用;##【3】方法参数共有两种类型:type1)基本数据类型(数字、布尔值);type2)对象引用;3.1)可以看到, 一个方法不可能修改一个基本数据类型的参数;

3.2)对象引用作为参数就不同了,可以很容易地利用下面的方法实现将一个雇员的薪水提高两倍:

package com.corejava;

public class EmployeeTest

{

public static void main(String[] args)

{

Employee e = new Employee(10);

Employee.tripleSalary(e);

System.out.PRintln("salary = " + e.getSalary());

}

}

class Employee

{

private double salary;

public Employee(double salary)

{

this.salary = salary;

}

public static void tripleSalary(Employee e)

{

e.raiseSalary(200);

}

public void raiseSalary(double x)

{

this.salary = this.salary * x / 100;

}

public double getSalary() {

return salary;

}

}

打印结果为: salary=20.0

3.3)上述程序的具体调用过程为:

step1)x 被初始化为 harry值的拷贝, 这里是一个对象的引用;step2)raiseSalary方法应用于这个对象引用, x 和 harry 同时引用的那个 Employee对象的薪水提高了200%;step3)方法结束后,参数变量x 不再使用, 当然,对象变量harry继续使用那个薪水涨了 200%的对象;3.4)读者已经看到,实现一个改变对象参数状态的方法并不是难事, 方法得到的是对象引用的copy, 对象引用和其它的copy同时引用同一个对象;##【4】再看个荔枝:```java

package com.corejava;public class EmployeeTestOne

{

public static void main(String[] args) {

EmployeeOne a = new EmployeeOne("Alice");

EmployeeOne b = new EmployeeOne("Bob");

System.out.println("before:" + a.getName() + b.getName());

EmployeeOne.swap(a,b);

System.out.println("after:" + a.getName() + b.getName());

}

}

class EmployeeOne

{

private String name;

public EmployeeOne(String name)

{

this.name = name;

}

public static void swap(EmployeeOne x, EmployeeOne y)

{

EmployeeOne temp = x;

x = y;

y = temp;

}

public String getName() {

return name;

}

}

打印结果为:

before:AliceBob

after:AliceBob

<font size=4> **4.1)**显然, 方法并没有改变存储在变量 a 和 b 中的 对象引用;swap 方法的参数x 和 y 被初始化为两个对象引用的copy, 这个方法交换的是 两个拷贝;在方法结束时参数变量x 和 y 被丢弃了, 原来的变量 a 和 b仍然引用这个方法调用之前所引用的对象;![这里写图片描述](http://img.blog.csdn.net/20151026094748165)

<font size=4>**4.2)这个过程说明:** java程序设计语言对对象采用的不是引用调用, 实际上,对象引用进行的是 值传递;

##**【5】下面总结下 java 中方法参数的使用情况:**

* <font size=3>**5.1)**一个方法不能修改一个基本数据类型的参数(数值型和布尔型);

* <font size=3>**5.2)**一个方法可以改变一个对象参数的状态;

* <font size=3>**5.3)**一个方法不能让对象参数引用一个新的对象;

<hr/>

##**【6】最后一个综合性荔枝:**

```java

/**

* This program demonstrates parameter passing in 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

{

private String name;

private double salary;

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;

}

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航