国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

方法參數(shù)的值調(diào)用+引用調(diào)用

2019-11-14 15:18:30
字體:
供稿:網(wǎng)友

##【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ù)值增大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í)行過程:

  • step1) x 被初始化percent值的一個copy;
  • step2) x 被乘以 3 后等于 30, 但是 percent 仍然是 10;
  • step3) 這個方法結(jié)束后, 參數(shù)變量x 不在使用;這里寫圖片描述
    ##【3】方法參數(shù)共有兩種類型:
  • type1) 基本數(shù)據(jù)類型(數(shù)字、布爾值);
  • type2) 對象引用;

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)用過程為:

  • step1) x 被初始化為 harry值的拷貝, 這里是一個對象的引用;
  • step2) raiseSalary方法應用于這個對象引用, x 和 harry 同時引用的那個 Employee對象的薪水提高了200%;
  • step3) 方法結(jié)束后,參數(shù)變量x 不再使用, 當然,對象變量harry繼續(xù)使用那個薪水漲了 200%的對象;這里寫圖片描述3.4) 讀者已經(jīng)看到,實現(xiàn)一個改變對象參數(shù)狀態(tài)的方法并不是難事, 方法得到的是對象引用的copy, 對象引用和其它的copy同時引用同一個對象;
    ##【4】再看個荔枝:```javapackage 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;}

}打印結(jié)果為:before:AliceBobafter:AliceBob

<font size=4> **4.1)**顯然, 方法并沒有改變存儲在變量 a 和 b 中的 對象引用;swap 方法的參數(shù)x 和 y 被初始化為兩個對象引用的copy, 這個方法交換的是 兩個拷貝;在方法結(jié)束時參數(shù)變量x 和 y 被丟棄了, 原來的變量 a 和 b仍然引用這個方法調(diào)用之前所引用的對象;![這里寫圖片描述](http://img.blog.csdn.net/20151026094748165)<font size=4>**4.2)這個過程說明:** java程序設計語言對對象采用的不是引用調(diào)用, 實際上,對象引用進行的是 值傳遞;##**【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;   }}

這里寫圖片描述


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 瑞安市| 东阳市| 塔河县| 中阳县| 上饶县| 万荣县| 台东市| 两当县| 高唐县| 富蕴县| 广东省| 奉化市| 康马县| 英吉沙县| 伽师县| 新平| 亚东县| 高淳县| 藁城市| 拉萨市| 六枝特区| 万宁市| 沁阳市| 秦皇岛市| 疏附县| 福海县| 威宁| 怀仁县| 商水县| 清水县| 苗栗市| 乐清市| 马公市| 错那县| 盐源县| 合肥市| 贡觉县| 吴堡县| 廊坊市| 蛟河市| 高唐县|