##【0】README
0.1)本文描述+源代碼均 轉(zhuǎn)自 core java volume 1, 旨在理清值調(diào)用+引用調(diào)用;
##【1】參數(shù)傳遞給方法的專業(yè)術(shù)語:
1.1)值調(diào)用:它表示方法接收的是調(diào)用者提供的值;
1.2)引用調(diào)用:它表示方法接收的是調(diào)用者提供的變量地址;
##【2】看個荔枝:
2.1)設(shè)一個方法視圖將一個參數(shù)值增大3倍:
public static void tripleValue(double x){ x = 3 * x;}
2.2)然后調(diào)用這個方法:
double percent = 10; tripleValue(percent);
2.3)無論如何,調(diào)用這個方法后,percent的值還是10,下面看一下具體執(zhí)行過程:
3.1)可以看到, 一個方法不可能修改一個基本數(shù)據(jù)類型的參數(shù);
3.2)對象引用作為參數(shù)就不同了,可以很容易地利用下面的方法實現(xiàn)將一個雇員的薪水提高兩倍:
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; } }打印結(jié)果為: salary=20.0
3.3)上述程序的具體調(diào)用過程為:
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;}
}打印結(jié)果為:before:AliceBobafter:AliceBob
<font size=4> **4.1)**顯然, 方法并沒有改變存儲在變量 a 和 b 中的 對象引用;swap 方法的參數(shù)x 和 y 被初始化為兩個對象引用的copy, 這個方法交換的是 兩個拷貝;在方法結(jié)束時參數(shù)變量x 和 y 被丟棄了, 原來的變量 a 和 b仍然引用這個方法調(diào)用之前所引用的對象;<font size=4>**4.2)這個過程說明:** java程序設(shè)計語言對對象采用的不是引用調(diào)用, 實際上,對象引用進(jìn)行的是 值傳遞;##**【5】下面總結(jié)下 java 中方法參數(shù)的使用情況:*** <font size=3>**5.1)**一個方法不能修改一個基本數(shù)據(jù)類型的參數(shù)(數(shù)值型和布爾型);* <font size=3>**5.2)**一個方法可以改變一個對象參數(shù)的狀態(tài);* <font size=3>**5.3)**一個方法不能讓對象參數(shù)引用一個新的對象;<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; }}
新聞熱點
疑難解答