更新時(shí)間:2020-09-17 15:26:38 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽3425次
我們什么時(shí)候需要構(gòu)造函數(shù)重載?
有時(shí)候需要用不同的方式初始化一個(gè)對(duì)象。這可以使用構(gòu)造函數(shù)重載來(lái)完成。例如,Thread類有8種類型的構(gòu)造函數(shù)。如果我們不想指定某個(gè)線程的任何內(nèi)容,那么我們可以簡(jiǎn)單地使用Thread類的默認(rèn)構(gòu)造函數(shù),但是如果我們需要指定線程名稱,那么我們可以使用String參數(shù)來(lái)調(diào)用Thread類的參數(shù)化構(gòu)造函數(shù),如下所示:
Thread?t=?new?Thread?("?MyThread?");
讓我們舉一個(gè)例子來(lái)理解構(gòu)造函數(shù)重載的需要。考慮以下只有一個(gè)構(gòu)造函數(shù)帶三個(gè)參數(shù)的類Box的實(shí)現(xiàn)。
//?An?example?class?to?understand?need?of
//?constructor?overloading.
class?Box
{
????double?width,?height,depth;
?
????//?constructor?used?when?all?dimensions
????//?specified
????Box(double?w,?double?h,?double?d)
????{
????????width?=?w;
????????height?=?h;
????????depth?=?d;
????}
?
????//?compute?and?return?volume
????double?volume()
????{
????????return?width?*?height?*?depth;
????}
}
我們可以看到Box()構(gòu)造函數(shù)需要三個(gè)參數(shù)。這意味著B(niǎo)ox對(duì)象的所有聲明必須將三個(gè)參數(shù)傳遞給Box()構(gòu)造函數(shù)。例如,以下語(yǔ)句目前無(wú)效:
Box ob = new Box();
由于Box()需要三個(gè)參數(shù),因此在沒(méi)有它們的情況下調(diào)用它是錯(cuò)誤的。假設(shè)我們只需要一個(gè)沒(méi)有初始維度的盒子對(duì)象,或者想要通過(guò)只指定一個(gè)將用于所有三個(gè)維度的值來(lái)初始化一個(gè)多維數(shù)據(jù)集。從Box類的上述實(shí)現(xiàn)中,我們無(wú)法使用這些選項(xiàng)。
這些類型的初始化對(duì)象的不同方式的問(wèn)題可以通過(guò)構(gòu)造函數(shù)重載來(lái)解決。下面是帶構(gòu)造函數(shù)重載的類Box的改進(jìn)版本。
//?Java?program?to?illustrate
//?Constructor?Overloading
class?Box
{
????double?width,?height,?depth;
?
????//?constructor?used?when?all?dimensions
????//?specified
????Box(double?w,?double?h,?double?d)
????{
????????width?=?w;
????????height?=?h;
????????depth?=?d;
????}
?
????//?constructor?used?when?no?dimensions
????//?specified
????Box()
????{
????????width?=?height?=?depth?=?0;
????}
?
????//?constructor?used?when?cube?is?created
????Box(double?len)
????{
????????width?=?height?=?depth?=?len;
????}
?
????//?compute?and?return?volume
????double?volume()
????{
????????return?width?*?height?*?depth;
????}
}
?
//?Driver?code
public?class?Test
{
????public?static?void?main(String?args[])
????{
????????//?create?boxes?using?the?various
????????//?constructors
????????Box?mybox1?=?new?Box(10,?20,?15);
????????Box?mybox2?=?new?Box();
????????Box?mycube?=?new?Box(7);
?
????????double?vol;
?
????????//?get?volume?of?first?box
????????vol?=?mybox1.volume();
????????System.out.println("?Volume?of?mybox1?is?"?+?vol);
?
????????//?get?volume?of?second?box
????????vol?=?mybox2.volume();
????????System.out.println("?Volume?of?mybox2?is?"?+?vol);
?
????????//?get?volume?of?cube
????????vol?=?mycube.volume();
????????System.out.println("?Volume?of?mycube?is?"?+?vol);
????}
}
輸出:
Volume?of?mybox1?is?3000.0
Volume?of?mybox2?is?-1.0
Volume?of?mycube?is?343.0
在構(gòu)造函數(shù)重載中使用this()
可以在構(gòu)造函數(shù)重載期間使用this()引用來(lái)從參數(shù)化構(gòu)造函數(shù)中隱式調(diào)用默認(rèn)構(gòu)造函數(shù)。請(qǐng)注意,this()應(yīng)該是構(gòu)造函數(shù)中的第一條語(yǔ)句。
//?Java?program?to?illustrate?role?of?this()?in
//?Constructor?Overloading
class?Box
{
????double?width,?height,?depth;
????int?boxNo;
?
????//?constructor?used?when?all?dimensions?and
????//?boxNo?specified
????Box(double?w,?double?h,?double?d,?int?num)
????{
????????width?=?w;
????????height?=?h;
????????depth?=?d;
????????boxNo?=?num;
????}
?
????//?constructor?used?when?no?dimensions?specified
????Box()
????{
????????//?an?empty?box
????????width?=?height?=?depth?=?0;
????}
?
????//?constructor?used?when?only?boxNo?specified
????Box(int?num)
????{
????????//?this()?is?used?for?calling?the?default
????????//?constructor?from?parameterized?constructor
????????this();
?
????????boxNo?=?num;
????}
?
????public?static?void?main(String[]?args)
????{
????????//?create?box?using?only?boxNo
????????Box?box1?=?new?Box(1);
?
????????//?getting?initial?width?of?box1
????????System.out.println(box1.width);
????}
}
輸出:
0.0
正如我們?cè)谏厦娴某绦蛑锌吹降哪菢樱覀冊(cè)趯?duì)象創(chuàng)建期間僅使用框編號(hào)調(diào)用Box(int num)構(gòu)造函數(shù)。通過(guò)在其中使用this()語(yǔ)句,默認(rèn)的構(gòu)造函數(shù)(Box())將從其中隱式調(diào)用,它將使用-1初始化Box的尺寸。
注意:構(gòu)造函數(shù)調(diào)用應(yīng)該是構(gòu)造函數(shù)體中的第一條語(yǔ)句。例如,以下片段無(wú)效并引發(fā)編譯時(shí)錯(cuò)誤。
Box(int?num)
{
????boxNo?=?num;
????/?*構(gòu)造函數(shù)調(diào)用必須是第一個(gè)
???????語(yǔ)句在構(gòu)造函數(shù)中*?/
????this();?/*錯(cuò)誤*/
}
在構(gòu)造函數(shù)重載時(shí)要注意的重點(diǎn):
●調(diào)用構(gòu)造函數(shù)必須是Java中構(gòu)造函數(shù)的第一條語(yǔ)句。
●如果我們已經(jīng)定義了任何參數(shù)化構(gòu)造函數(shù),那么編譯器不會(huì)創(chuàng)建默認(rèn)構(gòu)造函數(shù)。反之亦然,如果我們沒(méi)有定義任何構(gòu)造函數(shù),編譯器在編譯過(guò)程中會(huì)默認(rèn)創(chuàng)建默認(rèn)構(gòu)造函數(shù)(也稱為no-arg構(gòu)造函數(shù))
●在java中,遞歸構(gòu)造函數(shù)調(diào)用無(wú)效。
構(gòu)造函數(shù)重載與方法重載
嚴(yán)格來(lái)說(shuō),構(gòu)造函數(shù)重載與方法重載有點(diǎn)類似。如果我們想要使用不同數(shù)量的參數(shù)來(lái)初始化一個(gè)對(duì)象,那么當(dāng)我們需要基于不同參數(shù)的方法的不同定義時(shí),我們必須執(zhí)行構(gòu)造函數(shù)重載,因?yàn)槲覀儠?huì)重載方法。

以上就是動(dòng)力節(jié)點(diǎn)java培訓(xùn)機(jī)構(gòu)的小編針對(duì)“Java中的構(gòu)造函數(shù)重載教學(xué)”的內(nèi)容進(jìn)行的回答,希望對(duì)大家有所幫助,如有疑問(wèn),請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為你服務(wù)。
相關(guān)閱讀
Java實(shí)驗(yàn)班
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
Java就業(yè)班
有基礎(chǔ) 直達(dá)就業(yè)
Java夜校直播班
業(yè)余時(shí)間 高薪轉(zhuǎn)行
Java在職加薪班
工作1~3年,加薪神器
Java架構(gòu)師班
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)