欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

一、方法(Method)概念

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

  • 訪問修飾符有4種:public、protected、default、private【后期會(huì)詳細(xì)說明】
  • 返回值類型可能是8大基本數(shù)據(jù)類型、引用類型或無返回值(void
  • 方法名需符合標(biāo)識(shí)符命名規(guī)范、方法名需見名知意、方法名需是小駝峰(類名是大駝峰)
  • 參數(shù)列表是該方法需要調(diào)用者傳入的值(包括參數(shù)類型和參數(shù)名)【后期會(huì)詳細(xì)說明】
  • 方法體中才可編寫 Java 語句(并不是所有花括號(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 語句的花括號(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 語句的 case 語句塊是方法體");
            }
        }

        // 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ù)的和(通過數(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 的方法體中通過 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è)【值】都沒有
        // 的時(shí)候, 返回值是可變參數(shù)類型的默認(rèn)值
        int sum = vp.sumByVariable1Parameter(2, 5, 6, 7, 66, 53);
        // sumByVariable1Parameter = 139
        System.out.println("sumByVariable1Parameter = " + sum);
    }

    /**
     * 計(jì)算多個(gè)整數(shù)的和(通過可變參數(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ù)傳遞問題

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)通過 value 傳遞給方法。這意味著對參數(shù)值的任何更改僅存在于該方法的作用域內(nèi)。當(dāng)方法返回后,棧幀銷毀后,參數(shù)消失后,對它們的任何更改都將無效。

public class ArgumentsPassingTest {

    public static void main(String[] args) {
        int n = 10;
        test(n); // 值傳遞(v 和 n 沒有關(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ù)(例如對象)也按值傳遞給方法。這意味著當(dāng)方法返回時(shí),傳入的引用仍然引用著與之前相同的對象。但是,如果對象字段的值具有適當(dāng)?shù)?strong>訪問級(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;
    }
}

棧幀銷毀銷毀的是局部變量信息,堆空間的對象不會(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.

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

重載:

  • ① 方法名相同,參數(shù)類型或數(shù)量不同;
  • ② 重載與返回值類型、參數(shù)名稱無關(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器無法區(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ù)的傳遞問題,方法重載,方法簽名)的文章就介紹到這了,更多相關(guān)Java方法定義內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java實(shí)現(xiàn)復(fù)制文件并命名的超簡潔寫法

    Java實(shí)現(xiàn)復(fù)制文件并命名的超簡潔寫法

    這篇文章主要介紹了Java實(shí)現(xiàn)復(fù)制文件并命名的超簡潔寫法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 新手學(xué)習(xí)微服務(wù)SpringCloud項(xiàng)目架構(gòu)搭建方法

    新手學(xué)習(xí)微服務(wù)SpringCloud項(xiàng)目架構(gòu)搭建方法

    這篇文章主要介紹了新手學(xué)習(xí)微服務(wù)SpringCloud項(xiàng)目架構(gòu)搭建方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Java超詳細(xì)講解三大特性之一的繼承

    Java超詳細(xì)講解三大特性之一的繼承

    繼承就是可以直接使用前輩的屬性和方法。自然界如果沒有繼承,那一切都是處于混沌狀態(tài)。多態(tài)是同一個(gè)行為具有多個(gè)不同表現(xiàn)形式或形態(tài)的能力。多態(tài)就是同一個(gè)接口,使用不同的實(shí)例而執(zhí)行不同操作
    2022-05-05
  • Spring利用注解整合Mybatis的方法詳解

    Spring利用注解整合Mybatis的方法詳解

    這篇文章主要為大家介紹了Spring如何利用注解整合MyBatis,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-06-06
  • Spring Data JPA例子代碼[基于Spring Boot、Mysql]

    Spring Data JPA例子代碼[基于Spring Boot、Mysql]

    這篇文章主要介紹了Spring Data JPA例子代碼[基于Spring Boot、Mysql],小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • 帶大家深入了解Spring事務(wù)

    帶大家深入了解Spring事務(wù)

    Spring框架提供統(tǒng)一的事務(wù)抽象,通過統(tǒng)一的編程模型使得應(yīng)用程序可以很容易地在不同的事務(wù)框架之間進(jìn)行切換. 在學(xué)習(xí)Spring事務(wù)前,我們先對數(shù)據(jù)庫事務(wù)進(jìn)行簡單的介紹。,需要的朋友可以參考下
    2021-05-05
  • KotlinScript構(gòu)建SpringBootStarter保姆級(jí)教程

    KotlinScript構(gòu)建SpringBootStarter保姆級(jí)教程

    這篇文章主要為大家介紹了KotlinScript構(gòu)建SpringBootStarter的保姆級(jí)教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • java將數(shù)據(jù)寫入內(nèi)存,磁盤的方法

    java將數(shù)據(jù)寫入內(nèi)存,磁盤的方法

    下面小編就為大家分享一篇java將數(shù)據(jù)寫入內(nèi)存,磁盤的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • Java用 Rhino/Nashorn 代替第三方 JSON 轉(zhuǎn)換庫

    Java用 Rhino/Nashorn 代替第三方 JSON 轉(zhuǎn)換庫

    本篇文章主要介紹了Java用 Rhino/Nashorn 代替第三方 JSON 轉(zhuǎn)換庫,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-05-05
  • Map與JavaBean相互轉(zhuǎn)換的工具類?

    Map與JavaBean相互轉(zhuǎn)換的工具類?

    這篇文章主要介紹了Map與JavaBean相互轉(zhuǎn)換的工具類,在做導(dǎo)入的時(shí)候,遇到了需要將map對象轉(zhuǎn)化?成javabean的問題,也就是說,不清楚javabean的內(nèi)部字段排列,只知道m(xù)ap的?key代表javabean的字段名,value代表值,需要的朋友可以參考下
    2022-02-02

最新評論