最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java?方法(方法的定義,可變參數(shù),參數(shù)的傳遞問(wèn)題,方法重載,方法簽名)

 更新時(shí)間:2022年09月09日 17:01:41   作者:new?Handsome()  
這篇文章主要介紹了Java?方法(方法的定義,可變參數(shù),參數(shù)的傳遞問(wèn)題,方法重載,方法簽名),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下

一、方法(Method)概念

  •  1、Java 中的方法就是其他編程語(yǔ)言中的函數(shù)(Function)
  •  2、方法的定義格式:

  • 訪問(wèn)修飾符有4種:public、protected、default、private【后期會(huì)詳細(xì)說(shuō)明】
  • 返回值類型可能是8大基本數(shù)據(jù)類型、引用類型或無(wú)返回值(void
  • 方法名需符合標(biāo)識(shí)符命名規(guī)范、方法名需見(jiàn)名知意、方法名需是小駝峰(類名是大駝峰)
  • 參數(shù)列表是該方法需要調(diào)用者傳入的值(包括參數(shù)類型和參數(shù)名)【后期會(huì)詳細(xì)說(shuō)明】
  • 方法體中才可編寫 Java 語(yǔ)句(并不是所有花括號(hào)中都是方法體:如類定義的花括號(hào)中不是方法體)

下面是方法體代碼案例:

public class MethodBody {

    // 1.代碼塊
    {
        System.out.println("【{}】是方法體");
    }

    // 2.靜態(tài)代碼塊
    static {
        System.out.println("【static {}】是方法體");
    }

    // 3.方法
    public void run(int age) {
        System.out.println("方法的花括號(hào)中是方法體");

        // 4.if
        if (age == 18) {
            System.out.println("if 語(yǔ)句的花括號(hào)中是方法體");
        }

        // 5.for
        for (int i = 0; i < age; i++) {
            System.out.println("for 循環(huán)的花括號(hào)中是方法體");
        }

        // 6.while
        while (age > 50) {
            System.out.println("while 循環(huán)的花括號(hào)中是方法體");
        }

        // 7.switch-case
        switch (age) {
            // 錯(cuò)誤:在該區(qū)域?qū)懘a是錯(cuò)誤的(該區(qū)域不是方法體)
            // System.out.println(age); // ERROR
            case 1: {
                System.out.println("switch 語(yǔ)句的 case 語(yǔ)句塊是方法體");
            }
        }

        // 8.do-while
        do {
            System.out.println("do-while 循環(huán)的花括號(hào)中是方法體");
        } while (age < 5);
    }

}

其實(shí)可以理解為只有三個(gè)地方是代碼塊:

① 代碼塊
② 靜態(tài)代碼塊
③ 方法中
但是,當(dāng)初老師教的時(shí)候把 if、while、for 等也歸納為方法體

補(bǔ)充:定義方法可能還會(huì)有其他修飾符(eg:static、final、abstract),后面還會(huì)詳細(xì)介紹

仔細(xì)看下面的代碼, 學(xué)會(huì)定義方法:

public class CreateMethodDemo {
    public static void main(String[] args) {
        int sum1 = CreateMethodDemo.sumOne2Hundred(1, 100);
        // sum1 = 5050
        System.out.println("sum1 = " + sum1);

        int sum2 = CreateMethodDemo.sumOne2Hundred(1, 1000);
        // sum2 = 500500
        System.out.println("sum2 = " + sum2);

        int sum3 = CreateMethodDemo.sumOne2Hundred(1, 10000);
        // sum3 = 50005000
        System.out.println("sum3 = " + sum3);
    }
    /**
     * 計(jì)算[start, end]的累加和
     *
     * @param start 起始值
     * @param end   結(jié)束值
     * @return [start, end]的累加和
     */
    private static int sumOne2Hundred(int start, int end) {
        int sum = 0;

        for (int i = start; i <= end; i++) {
            sum += i;
        }
        return sum;
    }
}

二、可變參數(shù)(Variable)

思考:編寫程序計(jì)算多個(gè)整數(shù)的和。eg:計(jì)算【2, 5, 6, 7, 66, 53】的和

public class VariableParameter {
    public static void main(String[] args) {
        int[] arr = {2, 5, 6, 7, 66, 53};
        VariableParameter vp = new VariableParameter();
        // sumByArr = 139
        System.out.println(vp.sumByArr(arr));
    }

    /**
     * 計(jì)算多個(gè)整數(shù)的和(通過(guò)數(shù)組)
     *
     * @param arr (數(shù)組中存放需要進(jìn)行求和的多個(gè)整數(shù))
     * @return 數(shù)組中多個(gè)整數(shù)的和(類型是字符串)
     */
    private String sumByArr(int[] arr) {
        if (arr == null || arr.length < 1) return "arr 數(shù)組為 null, 為數(shù)組元素為 0";

        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return "sumByArr = " + sum; 
    }
}

思路1:
??可把需要進(jìn)行求和的整數(shù)放入一個(gè)整型數(shù)組中,并把整型數(shù)組作為參數(shù)傳給 sumByArr 方法
??sumByArr 方法接收一個(gè) int 類型的數(shù)組作為參數(shù),在 sumByArr 的方法體中通過(guò) for 循環(huán)遍歷數(shù)組中的數(shù)字,并進(jìn)行求和
思路2:
??使用可變參數(shù)替換 arr 數(shù)組

public class VariableParameter {

    public static void main(String[] args) {
        VariableParameter vp = new VariableParameter();
        // 當(dāng) sumByVariable1Parameter 的參數(shù)列表中一個(gè)【值】都沒(méi)有
        // 的時(shí)候, 返回值是可變參數(shù)類型的默認(rèn)值
        int sum = vp.sumByVariable1Parameter(2, 5, 6, 7, 66, 53);
        // sumByVariable1Parameter = 139
        System.out.println("sumByVariable1Parameter = " + sum);
    }

    /**
     * 計(jì)算多個(gè)整數(shù)的和(通過(guò)可變參數(shù))
     *
     * @param nums (參數(shù)列表中可以放多個(gè)需要進(jìn)行求和的整數(shù))
     * @return 參數(shù)列表中多個(gè)整數(shù)的和(類型 int)
     */
    private int sumByVariable1Parameter(int... nums) {
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
        }
        return sum;
    }
}
  • 可變參數(shù)的本質(zhì)是數(shù)組
  • 可變參數(shù)必須是方法的參數(shù)列表中的最后一個(gè)參數(shù)

String 類有靜態(tài)方法 format 可用于拼接字符,它的底層就用到了【可變參數(shù)】

public class VariableParameter {

    public static void main(String[] args) {
        String info = String.format("name: %s; age: %d; money: %f", 
    							"慶醫(yī)", 10, 895863.99);
        // info = name: 慶醫(yī); age: 10; money: 895863.990000
        System.out.println("info = " + info);
    }
}

三、方法的參數(shù)傳遞問(wèn)題

1. 基本數(shù)據(jù)類型

Passing Primitive Data Type Arguments 傳遞原始數(shù)據(jù)類型參數(shù)

  • 基本類型作為參數(shù)是值傳遞
  • 基本類型作為返回值,返回的是值本身
  • 基本類型:byte、short、int、long、float、double、boolean、char

Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost.

原始參數(shù)(eg:int 或 double)通過(guò) value 傳遞給方法。這意味著對(duì)參數(shù)值的任何更改僅存在于該方法的作用域內(nèi)。當(dāng)方法返回后,棧幀銷毀后,參數(shù)消失后,對(duì)它們的任何更改都將無(wú)效。

public class ArgumentsPassingTest {

    public static void main(String[] args) {
        int n = 10;
        test(n); // 值傳遞(v 和 n 沒(méi)有關(guān)系)
        // n = 10
        System.out.println("n = " + n);
    }
    private static void test(int v) { // v = 10
        v = 20;
    }
}

基本類型作為返回值,返回的是值本身:

public class ArgumentsPassingTest {

    public static void main(String[] args) {
        int test = test(66);
        // test = 88
        System.out.println("test = " + test);
    }

    private static int test(int param) {
        param = 88;
        return param;
    }
}

2. 引用數(shù)據(jù)類型

Passing Reference Data Type Arguments(傳遞引用數(shù)據(jù)類型參數(shù))

  • 引用類型作為參數(shù)是引用傳遞(地址傳遞)
  • 引用類型作為返回值是引用傳遞(地址傳遞

Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object’s fields can be changed in the method, if they have the proper access level.
引用數(shù)據(jù)類型參數(shù)(例如對(duì)象)也按值傳遞給方法。這意味著當(dāng)方法返回時(shí),傳入的引用仍然引用著與之前相同的對(duì)象。但是,如果對(duì)象字段的值具有適當(dāng)?shù)?strong>訪問(wèn)級(jí)別,則可以在方法中更改它們。

public class ArgumentsPassingTest {

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        test(nums);
        // nums = [1, 66, 3]
        System.out.println("nums = " + Arrays.toString(nums));
    }
    private static void test(int[] param) {
        param[1] = 66;
    }
}

引用類型作為返回值是引用傳遞(地址傳遞):

public class ArgumentsPassingTest {

    public static void main(String[] args) {
        int[] test = test();

        // test = [1314, 520, 666]
        System.out.println("test = " + Arrays.toString(test));
    }
    private static int[] test() {
        int[] ints = {1314, 520, 666};
        return ints;
    }
}

棧幀銷毀銷毀的是局部變量信息,堆空間的對(duì)象不會(huì)被回收的。

四、方法簽名(Method Signature)

方法簽名只由2部分組成:方法名、參數(shù)類型

private static void test(double pai, String name, int age) {
    return null;
}
  • 上面方法的方法簽名是:test(double, String, int)
  • 在同一個(gè)類中,方法簽名是唯一的(同一方法簽名在同一個(gè)類中只能出現(xiàn)一次)

五、方法的重載(Overload) 

官方教程:

Overloaded(重載) methods are differentiated by the number and the type of the arguments passed into the method. For example: run(String s) and run(int i) are distinct and unique methods because they require different argument types.

重載的方法通過(guò)傳遞給方法的參數(shù)的數(shù)量和類型來(lái)區(qū)分。
例如:run(String s)run(int i) 是不同且獨(dú)特的方法,因?yàn)樗鼈儞碛?strong>不同的參數(shù)類型。

重載:

  • ① 方法名相同,參數(shù)類型或數(shù)量不同;
  • ② 重載與返回值類型、參數(shù)名稱無(wú)關(guān)

The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
編譯器在區(qū)分方法時(shí)不考慮返回類型,因此即使它們具有不同的返回類型,也不能聲明具有相同簽名的兩個(gè)方法。

You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.
您不能聲明多個(gè)具有相同名稱和相同數(shù)量和類型的參數(shù)的方法,因?yàn)榫幾g器無(wú)法區(qū)分它們。

下面的兩個(gè)方法構(gòu)成方法重載:

private static int[] test(double weight, String name, int age) {
    return null;
}
private static int[] test(int age, double weight, String name) {
    return null;
}

到此這篇關(guān)于Java 方法(方法的定義,可變參數(shù),參數(shù)的傳遞問(wèn)題,方法重載,方法簽名)的文章就介紹到這了,更多相關(guān)Java方法定義內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Tornadofx學(xué)習(xí)筆記之IconTextFx開(kāi)源庫(kù)整合5000+個(gè)字體圖標(biāo)

    Tornadofx學(xué)習(xí)筆記之IconTextFx開(kāi)源庫(kù)整合5000+個(gè)字體圖標(biāo)

    這篇文章主要介紹了Tornadofx學(xué)習(xí)筆記之IconTextFx開(kāi)源庫(kù)整合5000+個(gè)字體圖標(biāo)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • 完整的醫(yī)院就診掛號(hào)系統(tǒng)基于Spring MVC + Spring + MyBatis實(shí)現(xiàn)

    完整的醫(yī)院就診掛號(hào)系統(tǒng)基于Spring MVC + Spring + MyBatis實(shí)現(xiàn)

    這篇文章主要介紹了基于Spring MVC + Spring + MyBatis實(shí)現(xiàn)的醫(yī)院就診掛號(hào)系統(tǒng),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • SpringBoot與SpringCache概念用法大全

    SpringBoot與SpringCache概念用法大全

    這篇文章主要介紹了SpringBoot與SpringCache的概念及基本用法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • Mysql?json類型字段Java+Mybatis數(shù)據(jù)字典功能的實(shí)踐方式

    Mysql?json類型字段Java+Mybatis數(shù)據(jù)字典功能的實(shí)踐方式

    這篇文章主要介紹了Mysql?json類型字段Java+Mybatis數(shù)據(jù)字典功能的實(shí)踐方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • elasticsearch集群cluster主要功能詳細(xì)分析

    elasticsearch集群cluster主要功能詳細(xì)分析

    這篇文章主要為大家介紹了elasticsearch集群cluster主要功能詳細(xì)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-04-04
  • SWT(JFace)體驗(yàn)之Icon任我變

    SWT(JFace)體驗(yàn)之Icon任我變

    SWT(JFace)體驗(yàn)之Icon任我變
    2009-06-06
  • Aspectj與Spring AOP的對(duì)比分析

    Aspectj與Spring AOP的對(duì)比分析

    這篇文章主要介紹了Aspectj與Spring AOP的對(duì)比分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Spring Boot加載配置文件的完整步驟

    Spring Boot加載配置文件的完整步驟

    這篇文章主要給大家介紹了關(guān)于Spring Boot加載配置文件的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Spring中AOP注解@Aspect的使用詳解

    Spring中AOP注解@Aspect的使用詳解

    這篇文章主要介紹了Spring中AOP注解@Aspect的使用詳解,AOP是種面向切面的編程思想,面向切面編程是將程序抽象成各個(gè)切面,將那些影響了多個(gè)類的公共行為抽取到一個(gè)可重用模塊里,減少系統(tǒng)的重復(fù)代碼,降低模塊間的耦合度,增強(qiáng)代碼的可操作性和可維護(hù)性,需要的朋友可以參考下
    2024-01-01
  • Java項(xiàng)目的目錄結(jié)構(gòu)詳解

    Java項(xiàng)目的目錄結(jié)構(gòu)詳解

    本文主要介紹了Java項(xiàng)目的目錄結(jié)構(gòu)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02

最新評(píng)論

鄂尔多斯市| 新闻| 兴安县| 五家渠市| 分宜县| 桐梓县| 太康县| 运城市| 九寨沟县| 延津县| 黄平县| 西充县| 浦北县| 阳西县| 石门县| 武义县| 巴马| 谢通门县| 新蔡县| 榆社县| 梧州市| 新晃| 博兴县| 都安| 湛江市| 青川县| 枣庄市| 黔西县| 开化县| 澄江县| 蚌埠市| 潞城市| 理塘县| 图木舒克市| 汶川县| 广昌县| 迁西县| 兰西县| 吉隆县| 和硕县| 南京市|