Java支持方法重載的原因
Java為什么要支持方法重載
為什么要使用重載?而不是把一個(gè)方法名字換成不同的。
任何編程語言中都具備的一項(xiàng)重要特性就是名稱。當(dāng)你創(chuàng)建一個(gè)對(duì)象時(shí),就會(huì)給此對(duì)象分配的內(nèi)存空間一個(gè)名稱。一個(gè)方法就是一種行為的名稱。通過名稱引用所各種對(duì)象,屬性和方法。良好的命名可以讓系統(tǒng)易于理解和修改。
將人類語言細(xì)微的差別映射到編程語言中會(huì)產(chǎn)生一個(gè)問題。通常,相同的詞可以表達(dá)多種不同的含義——它們被"重載"了。特別是當(dāng)含義的差別很小時(shí),這會(huì)更加有用。
你會(huì)說"清洗襯衫"、“清洗車"和"清洗狗”。而如果硬要這么說就會(huì)顯得很愚蠢:“以洗襯衫的方式洗襯衫”、“以洗車的方式洗車"和"以洗狗的方式洗狗”,因?yàn)槁牨姼静恍枰獏^(qū)分行為的動(dòng)作。大多數(shù)人類語言都具有"冗余"性,所以即使漏掉幾個(gè)詞,你也能明白含義。你不需要對(duì)每個(gè)概念都使用不同的詞匯——可以從上下文推斷出含義。
大多數(shù)編程語言(尤其是 C)要求為每個(gè)方法(在這些語言中經(jīng)常稱為函數(shù))提供一個(gè)獨(dú)一無二的標(biāo)識(shí)符。所以,你不能有一個(gè) print() 函數(shù)既能打印整型,也能打印浮點(diǎn)型——每個(gè)函數(shù)名都必須不同。
但在 Java (C++) 中,還有一個(gè)因素也促使了必須使用方法重載:構(gòu)造器。因?yàn)闃?gòu)造器方法名肯定與類名相同,所以一個(gè)類中只會(huì)有一個(gè)構(gòu)造器名。
那么你怎么通過不同方式創(chuàng)建一個(gè)對(duì)象?例如,你想創(chuàng)建一個(gè)類,這個(gè)類的初始化方式有兩種:一種是標(biāo)準(zhǔn)化方式,另一種是從文件中讀取信息的方式。你需要兩個(gè)構(gòu)造器:無參構(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ì)象既可以是一顆樹苗,使用無參構(gòu)造器,也可以是一顆在溫室中已長大的樹,已經(jīng)有一定高度,這就需要有參構(gòu)造器。
你也許想以多種方式調(diào)用 info()。比如,如果你想打印額外的消息,就可以使用 info(String) 方法。若你無話可說,就可以使用 info() 方法。用兩個(gè)命名定義完全相同的概念看起來很奇怪,而使用方法重載,你就可以使用一個(gè)命名來定義一個(gè)概念。
區(qū)分重載方法
如果兩個(gè)方法命名相同,Java是怎么知道你調(diào)用的是哪個(gè)呢?
有一條簡單的規(guī)則:每個(gè)被重載的方法必須有獨(dú)一無二的參數(shù)列表。
除了通過參數(shù)列表的不同來區(qū)分兩個(gè)相同命名的方法,其他也沒什么方式了。你甚至可以根據(jù)參數(shù)列表中的參數(shù)順序來區(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ì)有人困惑,“為什么只能通過類名和參數(shù)列表,不能通過方法的返回值區(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 在一些具體情形下提高了猜測的準(zhǔn)確度,但是通常來說并不起作用。
到此這篇關(guān)于Java支持方法重載的原因的文章就介紹到這了,更多相關(guān)Java方法重載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java日期時(shí)間格式化操作DateUtils 的整理
這篇文章主要介紹了Java日期時(shí)間格式化操作DateUtils 的整理的相關(guān)資料,這里總結(jié)了java日期格式化的操作,需要的朋友可以參考下2017-07-07
Java中多個(gè)線程交替循環(huán)執(zhí)行的實(shí)現(xiàn)
有些時(shí)候面試官經(jīng)常會(huì)問,兩個(gè)線程怎么交替執(zhí)行呀,本文就來詳細(xì)的介紹一下Java中多個(gè)線程交替循環(huán)執(zhí)行的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01
Spring?Boot開發(fā)RESTful接口與http協(xié)議狀態(tài)表述
這篇文章主要為大家介紹了Spring?Boot開發(fā)RESTful接口與http協(xié)議狀態(tài)表述,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
Java虛擬機(jī)使用jvisualvm工具遠(yuǎn)程監(jiān)控tomcat內(nèi)存
這篇文章主要介紹了Java虛擬機(jī)使用jvisualvm工具遠(yuǎn)程監(jiān)控tomcat內(nèi)存,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11

