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

Java支持方法重載的原因

 更新時(shí)間:2021年06月15日 11:30:05   作者:JavaEdge.  
今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著Java方法重載展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下

Java為什么要支持方法重載

為什么要使用重載?而不是把一個(gè)方法名字換成不同的。

任何編程語(yǔ)言中都具備的一項(xiàng)重要特性就是名稱。當(dāng)你創(chuàng)建一個(gè)對(duì)象時(shí),就會(huì)給此對(duì)象分配的內(nèi)存空間一個(gè)名稱。一個(gè)方法就是一種行為的名稱。通過(guò)名稱引用所各種對(duì)象,屬性和方法。良好的命名可以讓系統(tǒng)易于理解和修改。

將人類語(yǔ)言細(xì)微的差別映射到編程語(yǔ)言中會(huì)產(chǎn)生一個(gè)問(wèn)題。通常,相同的詞可以表達(dá)多種不同的含義——它們被"重載"了。特別是當(dāng)含義的差別很小時(shí),這會(huì)更加有用。

你會(huì)說(shuō)"清洗襯衫"、“清洗車"和"清洗狗”。而如果硬要這么說(shuō)就會(huì)顯得很愚蠢:“以洗襯衫的方式洗襯衫”、“以洗車的方式洗車"和"以洗狗的方式洗狗”,因?yàn)槁牨姼静恍枰獏^(qū)分行為的動(dòng)作。大多數(shù)人類語(yǔ)言都具有"冗余"性,所以即使漏掉幾個(gè)詞,你也能明白含義。你不需要對(duì)每個(gè)概念都使用不同的詞匯——可以從上下文推斷出含義。

大多數(shù)編程語(yǔ)言(尤其是 C)要求為每個(gè)方法(在這些語(yǔ)言中經(jīng)常稱為函數(shù))提供一個(gè)獨(dú)一無(wú)二的標(biāo)識(shí)符。所以,你不能有一個(gè) print() 函數(shù)既能打印整型,也能打印浮點(diǎn)型——每個(gè)函數(shù)名都必須不同。

但在 Java (C++) 中,還有一個(gè)因素也促使了必須使用方法重載:構(gòu)造器。因?yàn)闃?gòu)造器方法名肯定與類名相同,所以一個(gè)類中只會(huì)有一個(gè)構(gòu)造器名。
那么你怎么通過(guò)不同方式創(chuàng)建一個(gè)對(duì)象?例如,你想創(chuàng)建一個(gè)類,這個(gè)類的初始化方式有兩種:一種是標(biāo)準(zhǔn)化方式,另一種是從文件中讀取信息的方式。你需要兩個(gè)構(gòu)造器:無(wú)參構(gòu)造器和有一個(gè) String 類型參數(shù)的構(gòu)造器,該參數(shù)傳入文件名。兩個(gè)構(gòu)造器具有相同的名字——與類名相同。因此,方法重載是必要的,它允許方法具有相同的方法名但接收的參數(shù)不同。盡管方法重載對(duì)于構(gòu)造器很重要,但也可以對(duì)任何方法很方便地進(jìn)行重載。

重載構(gòu)造器和方法:

class Tree {
    int height;
    Tree() {
        System.out.println("Planting a seedling");
        height = 0;
    }
    Tree(int initialHeight) {
        height = initialHeight;
        System.out.println("Creating new Tree that is " + height + " feet tall");
    }
    void info() {
        System.out.println("Tree is " + height + " feet tall");
    }
    void info(String s) {
        System.out.println(s + ": Tree is " + height + " feet tall");
    }
}

public class Overloading {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            Tree t = new Tree(i);
            t.info();
            t.info("overloaded method");
        }
        new Tree(); 
    }
}

一個(gè) Tree 對(duì)象既可以是一顆樹苗,使用無(wú)參構(gòu)造器,也可以是一顆在溫室中已長(zhǎng)大的樹,已經(jīng)有一定高度,這就需要有參構(gòu)造器。

你也許想以多種方式調(diào)用 info()。比如,如果你想打印額外的消息,就可以使用 info(String) 方法。若你無(wú)話可說(shuō),就可以使用 info() 方法。用兩個(gè)命名定義完全相同的概念看起來(lái)很奇怪,而使用方法重載,你就可以使用一個(gè)命名來(lái)定義一個(gè)概念。

區(qū)分重載方法

如果兩個(gè)方法命名相同,Java是怎么知道你調(diào)用的是哪個(gè)呢?
有一條簡(jiǎn)單的規(guī)則:每個(gè)被重載的方法必須有獨(dú)一無(wú)二的參數(shù)列表。

除了通過(guò)參數(shù)列表的不同來(lái)區(qū)分兩個(gè)相同命名的方法,其他也沒(méi)什么方式了。你甚至可以根據(jù)參數(shù)列表中的參數(shù)順序來(lái)區(qū)分不同的方法,盡管這會(huì)造成代碼難以維護(hù)。

重載與基本類型

基本類型可以自動(dòng)從小類型轉(zhuǎn)為大類型。當(dāng)這與重載結(jié)合時(shí),這會(huì)令人有點(diǎn)困惑:

public class PrimitiveOverloading {
    void f1(char x) {
        System.out.print("f1(char)");
    }
    void f1(byte x) {
        System.out.print("f1(byte)");
    }
    void f1(short x) {
        System.out.print("f1(short)");
    }
    void f1(int x) {
        System.out.print("f1(int)");
    }
    void f1(long x) {
        System.out.print("f1(long)");
    }
    void f1(float x) {
        System.out.print("f1(float)");
    }
    void f1(double x) {
        System.out.print("f1(double)");
    }
    void f2(byte x) {
        System.out.print("f2(byte)");
    }
    void f2(short x) {
        System.out.print("f2(short)");
    }
    void f2(int x) {
        System.out.print("f2(int)");
    }
    void f2(long x) {
        System.out.print("f2(long)");
    }
    void f2(float x) {
        System.out.print("f2(float)");
    }
    void f2(double x) {
        System.out.print("f2(double)");
    }
    void f3(short x) {
        System.out.print("f3(short)");
    }
    void f3(int x) {
        System.out.print("f3(int)");
    }
    void f3(long x) {
        System.out.print("f3(long)");
    }
    void f3(float x) {
        System.out.print("f3(float)");
    }
    void f3(double x) {
        System.out.print("f3(double)");
    }
    void f4(int x) {
        System.out.print("f4(int)");
    }
    void f4(long x) {
        System.out.print("f4(long)");
    }
    void f4(float x) {
        System.out.print("f4(float)");
    }
    void f4(double x) {
        System.out.print("f4(double)");
    }
    void f5(long x) {
        System.out.print("f5(long)");
    }
    void f5(float x) {
        System.out.print("f5(float)");
    }
    void f5(double x) {
        System.out.print("f5(double)");
    }
    void f6(float x) {
        System.out.print("f6(float)");
    }
    void f6(double x) {
        System.out.print("f6(double)");
    }
    void f7(double x) {
        System.out.print("f7(double)");
    }
    void testConstVal() {
        System.out.print("5: ");
        f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5);
        System.out.println();
    }
    void testChar() {
        char x = 'x';
        System.out.print("char: ");
        f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
        System.out.println();
    }
    void testByte() {
        byte x = 0;
        System.out.print("byte: ");
        f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
        System.out.println();
    }
    void testShort() {
        short x = 0;
        System.out.print("short: ");
        f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
        System.out.println();
    }
    void testInt() {
        int x = 0;
        System.out.print("int: ");
        f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
        System.out.println();
    }
    void testLong() {
        long x = 0;
        System.out.print("long: ");
        f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
        System.out.println();
    }
    void testFloat() {
        float x = 0;
        System.out.print("float: ");
        f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
        System.out.println();
    }
    void testDouble() {
        double x = 0;
        System.out.print("double: ");
        f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
        System.out.println();
    }

    public static void main(String[] args) {
        PrimitiveOverloading p = new PrimitiveOverloading();
        p.testConstVal();
        p.testChar();
        p.testByte();
        p.testShort();
        p.testInt();
        p.testLong();
        p.testFloat();
        p.testDouble();
    }
}

輸出:

5: f1(int)f2(int)f3(int)f4(int)f5(long)f6(float)f7(double)
char: f1(char)f2(int)f3(int)f4(int)f5(long)f6(float)f7(double)
byte: f1(byte)f2(byte)f3(short)f4(int)f5(long)f6(float)f7(double)
short: f1(short)f2(short)f3(short)f4(int)f5(long)f6(float)f7(double)
int: f1(int)f2(int)f3(int)f4(int)f5(long)f6(float)f7(double)
long: f1(long)f2(long)f3(long)f4(long)f5(long)f6(float)f7(double)
float: f1(float)f2(float)f3(float)f4(float)f5(float)f6(float)f7(double)
double: f1(double)f2(double)f3(double)f4(double)f5(double)f6(double)f7(double)

若傳入的參數(shù)類型大于方法期望接收的參數(shù)類型,你必須首先做縮窄轉(zhuǎn)換,否則編譯器就會(huì)報(bào)錯(cuò)。

返回值的重載

經(jīng)常會(huì)有人困惑,“為什么只能通過(guò)類名和參數(shù)列表,不能通過(guò)方法的返回值區(qū)分方法呢?”。例如以下兩個(gè)方法,它們有相同的命名和參數(shù),但是很容易區(qū)分:

void f(){}
int f() {return 1;}

有些情況下,編譯器很容易就可以從上下文準(zhǔn)確推斷出該調(diào)用哪個(gè)方法,如 int x = f()。

但是,你可以調(diào)用一個(gè)方法且忽略返回值。這叫做調(diào)用一個(gè)函數(shù)的副作用,因?yàn)槟悴辉诤醴祷刂?,只是想利用方法做些事。所以如果你直接調(diào)用 f(),Java 編譯器就不知道你想調(diào)用哪個(gè)方法,閱讀者也不明所以。因?yàn)檫@個(gè)原因,所以你不能根據(jù)返回值類型區(qū)分重載的方法。為了支持新特性,Java 8 在一些具體情形下提高了猜測(cè)的準(zhǔn)確度,但是通常來(lái)說(shuō)并不起作用。

到此這篇關(guān)于Java支持方法重載的原因的文章就介紹到這了,更多相關(guān)Java方法重載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用Java實(shí)現(xiàn)構(gòu)建jenkins的多個(gè)job并返回構(gòu)建結(jié)果示例

    使用Java實(shí)現(xiàn)構(gòu)建jenkins的多個(gè)job并返回構(gòu)建結(jié)果示例

    這篇文章主要介紹了使用Java實(shí)現(xiàn)構(gòu)建jenkins的多個(gè)job并返回構(gòu)建結(jié)果示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-05-05
  • Javaweb 鼠標(biāo)移入移出表格顏色變化的實(shí)現(xiàn)

    Javaweb 鼠標(biāo)移入移出表格顏色變化的實(shí)現(xiàn)

    這篇文章主要介紹了Javaweb 鼠標(biāo)移入移出表格顏色變化的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 不到十行實(shí)現(xiàn)javaCV圖片OCR文字識(shí)別

    不到十行實(shí)現(xiàn)javaCV圖片OCR文字識(shí)別

    識(shí)別圖片中的文字,會(huì)省很多時(shí)間,本文介紹了javaCV圖片OCR文字識(shí)別,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • 深入解析SpringBatch適配器

    深入解析SpringBatch適配器

    Spring Batch是Spring的一個(gè)子項(xiàng)目,使用Java語(yǔ)言并基于Spring框架為基礎(chǔ)開發(fā),使得已經(jīng)使用 Spring 框架的開發(fā)者或者企業(yè)更容易訪問(wèn)和利用企業(yè)服務(wù),本文給大家介紹SpringBatch適配器的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2021-11-11
  • Java System.setProperty()用法詳解

    Java System.setProperty()用法詳解

    這篇文章主要介紹了Java System.setProperty()用法詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Kryo序列化及反序列化用法示例

    Kryo序列化及反序列化用法示例

    這篇文章主要介紹了Kryo序列化及反序列化用法示例,小編覺(jué)得挺不錯(cuò)的,這里分享給大家,需要的朋友可以參考下。
    2017-10-10
  • Spring aop失效的幾種解決方案

    Spring aop失效的幾種解決方案

    這篇文章主要介紹了Spring aop失效的幾種解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • 詳解SpringBoot目錄結(jié)構(gòu)劃分

    詳解SpringBoot目錄結(jié)構(gòu)劃分

    代碼目錄結(jié)構(gòu)是一個(gè)在項(xiàng)目開發(fā)中非常重要的部分,本文主要介紹了詳解SpringBoot目錄結(jié)構(gòu)劃分,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-08-08
  • 解決spring-boot-starter-web等報(bào)紅問(wèn)題

    解決spring-boot-starter-web等報(bào)紅問(wèn)題

    這篇文章主要介紹了解決spring-boot-starter-web等報(bào)紅問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • SpringBoot中@RestControllerAdvice注解的使用

    SpringBoot中@RestControllerAdvice注解的使用

    這篇文章主要介紹了SpringBoot中@RestControllerAdvice注解的使用,@RestControllerAdvice主要用精簡(jiǎn)客戶端返回異常,它可以捕獲各種異常,需要的朋友可以參考下
    2024-01-01

最新評(píng)論