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
浙公网安备 33010602011771号