方法参数的值调用+引用调用+深浅拷贝
生活随笔
收集整理的這篇文章主要介紹了
方法参数的值调用+引用调用+深浅拷贝
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【0】README
0.1)本文描述+源代碼均 轉自 core java volume 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)對象引用作為參數就不同了,可以很容易地利用下面的方法實現將一個雇員的薪水提高兩倍:
3.3)上述程序的具體調用過程為:
- step1) x 被初始化為 harry值的拷貝, 這里是一個對象的引用;
- step2) raiseSalary方法應用于這個對象引用, x 和 harry 同時引用的那個 Employee對象的薪水提高了200%;
- step3) 方法結束后,參數變量x 不再使用, 當然,對象變量harry繼續使用那個薪水漲了 200%的對象;
3.4) 讀者已經看到,實現一個改變對象參數狀態的方法并不是難事, 方法得到的是對象引用的copy, 對象引用和其它的copy同時引用同一個對象;
【4】再看個荔枝:
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 4.1)顯然, 方法并沒有改變存儲在變量 a 和 b 中的 對象引用;swap 方法的參數x 和 y 被初始化為兩個對象引用的copy, 這個方法交換的是 兩個拷貝;在方法結束時參數變量x 和 y 被丟棄了, 原來的變量 a 和 b仍然引用這個方法調用之前所引用的對象;
4.2)這個過程說明: java程序設計語言對對象采用的不是引用調用, 實際上,對象引用進行的是 值傳遞;
【5】下面總結下 java 中方法參數的使用情況:
- 5.1)一個方法不能修改一個基本數據類型的參數(數值型和布爾型);
- 5.2)一個方法可以改變一個對象參數的狀態;
- 5.3)一個方法不能讓對象參數引用一個新的對象;
【6】最后一個綜合性荔枝:
/*** 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;} }總結
以上是生活随笔為你收集整理的方法参数的值调用+引用调用+深浅拷贝的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 360版三国杀苹果手机下载电脑版下载(三
- 下一篇: MarkdownPad 汉化破解(含下载