1. 程式人生 > 其它 >Core Java 學習筆記之 ch4.5 方法引數

Core Java 學習筆記之 ch4.5 方法引數

學習書本:  Core Java  8th Edition

4.5 方法引數 

C語言中的函式呼叫中,引數傳遞方式有兩種:傳值(call by value, 值呼叫)、傳地址(call by address/reference, 引用呼叫)

 實參、形參。  

值呼叫call by value表示方法接收的是呼叫者提供的值。 

引用呼叫call by reference)表示方法接收的是呼叫者提供的地址。

 C語言中,函式不能修改值呼叫所對應的實參的值,可以修改傳地址呼叫所對應的實參的值。

 Java中總是採用值呼叫。  也就是說,方法得到的是所有引數值的一個拷貝,特別是,方法不能修改傳遞給它的任何引數變數(實參)的內容(不能讓實參物件名指向另一個物件,但可以改變實參物件的成員變數的值

 例如,考慮下面的呼叫:

double  percent=10;

harry.raiseSalary(percent);

不必管這個方法的具體實現,在方法呼叫之後,percent的值還是10

 書中的一個構建得非常好的例子,例4-4: ParamTest.java

仔細閱讀此程式的程式程式碼和輸出結果。會發現有3個需要記住的重要結論

1. Methods can't modify numeric parameters / 方法不能修改數值型實參

2. Methods can change the state of object parameters / 方法可以修改物件實參的成員變數(實參物件的狀態)

3. Methods can't attach new objects to object parameters / 方法不能讓物件實參指向(attach)一個新的物件

 1 /**
 2  * This program demonstrates parameter passing in Java.  此程式演示Java中的引數傳遞
 3  * @version 1.00 2000-01-27
 4  * @author Cay Horstmann
 5  */
 6 public class ParamTest
 7 {
 8    public static void main(String[] args)
9 { 10 /* 11 * Test 1: Methods can't modify numeric parameters 測試1 :方法不能修改數值型實參 12 */ 13 System.out.println("Testing tripleValue:"); 14 double percent = 10; 15 System.out.println("Before: percent=" + percent); 16 tripleValue(percent); 17 System.out.println("After: percent=" + percent); 18 19 /* 20 * Test 2: Methods can change the state of object parameters 測試2:方法可以修改物件實參的成員變數(實參物件的狀態) 21 */ 22 System.out.println("\nTesting tripleSalary:"); 23 Employee harry = new Employee("Harry", 50000); 24 System.out.println("Before: salary=" + harry.getSalary()); 25 tripleSalary(harry); 26 System.out.println("After: salary=" + harry.getSalary()); 27 28 /* 29 * Test 3: Methods can't attach new objects to object parameters 測試3:方法不能讓物件實參指向(attach)一個新的物件 30 */ 31 System.out.println("\nTesting swap:"); 32 Employee a = new Employee("Alice", 70000); 33 Employee b = new Employee("Bob", 60000); 34 System.out.println("Before: a=" + a.getName()); 35 System.out.println("Before: b=" + b.getName()); 36 swap(a, b); 37 System.out.println("After: a=" + a.getName()); 38 System.out.println("After: b=" + b.getName()); 39 } 40 41 public static void tripleValue(double x) // doesn't work 42 { 43 x = 3 * x; 44 System.out.println("End of method: x=" + x); 45 } 46 47 public static void tripleSalary(Employee x) // works 48 { 49 x.raiseSalary(200); 50 System.out.println("End of method: salary=" + x.getSalary()); 51 } 52 53 public static void swap(Employee x, Employee y) 54 { 55 Employee temp = x; 56 x = y; 57 y = temp; 58 System.out.println("End of method: x=" + x.getName()); 59 System.out.println("End of method: y=" + y.getName()); 60 } 61 } 62 63 class Employee // simplified Employee class 64 { 65 public Employee(String n, double s) 66 { 67 name = n; 68 salary = s; 69 } 70 71 public String getName() 72 { 73 return name; 74 } 75 76 public double getSalary() 77 { 78 return salary; 79 } 80 81 public void raiseSalary(double byPercent) 82 { 83 double raise = salary * byPercent / 100; 84 salary += raise; 85 } 86 87 private String name; 88 private double salary; 89 }

該程式的輸出為:

 

Testing tripleValue:
Before: percent=10.0
End of method: x=30.0
After: percent=10.0

Testing tripleSalary:
Before: salary=50000.0
End of method: salary=150000.0
After: salary=150000.0

Testing swap:
Before: a=Alice
Before: b=Bob
End of method: x=Bob
End of method: y=Alice
After: a=Alice
After: b=Bob