更新時間:2020-09-17 15:26:38 來源:動力節(jié)點 瀏覽2862次
我們什么時候需要構(gòu)造函數(shù)重載?
有時候需要用不同的方式初始化一個對象。這可以使用構(gòu)造函數(shù)重載來完成。例如,Thread類有8種類型的構(gòu)造函數(shù)。如果我們不想指定某個線程的任何內(nèi)容,那么我們可以簡單地使用Thread類的默認(rèn)構(gòu)造函數(shù),但是如果我們需要指定線程名稱,那么我們可以使用String參數(shù)來調(diào)用Thread類的參數(shù)化構(gòu)造函數(shù),如下所示:
Thread?t=?new?Thread?("?MyThread?");
讓我們舉一個例子來理解構(gòu)造函數(shù)重載的需要??紤]以下只有一個構(gòu)造函數(shù)帶三個參數(shù)的類Box的實現(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ù)需要三個參數(shù)。這意味著Box對象的所有聲明必須將三個參數(shù)傳遞給Box()構(gòu)造函數(shù)。例如,以下語句目前無效:
Box ob = new Box();
由于Box()需要三個參數(shù),因此在沒有它們的情況下調(diào)用它是錯誤的。假設(shè)我們只需要一個沒有初始維度的盒子對象,或者想要通過只指定一個將用于所有三個維度的值來初始化一個多維數(shù)據(jù)集。從Box類的上述實現(xiàn)中,我們無法使用這些選項。
這些類型的初始化對象的不同方式的問題可以通過構(gòu)造函數(shù)重載來解決。下面是帶構(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()引用來從參數(shù)化構(gòu)造函數(shù)中隱式調(diào)用默認(rèn)構(gòu)造函數(shù)。請注意,this()應(yīng)該是構(gòu)造函數(shù)中的第一條語句。
//?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
正如我們在上面的程序中看到的那樣,我們在對象創(chuàng)建期間僅使用框編號調(diào)用Box(int num)構(gòu)造函數(shù)。通過在其中使用this()語句,默認(rèn)的構(gòu)造函數(shù)(Box())將從其中隱式調(diào)用,它將使用-1初始化Box的尺寸。
注意:構(gòu)造函數(shù)調(diào)用應(yīng)該是構(gòu)造函數(shù)體中的第一條語句。例如,以下片段無效并引發(fā)編譯時錯誤。
Box(int?num)
{
????boxNo?=?num;
????/?*構(gòu)造函數(shù)調(diào)用必須是第一個
???????語句在構(gòu)造函數(shù)中*?/
????this();?/*錯誤*/
}
在構(gòu)造函數(shù)重載時要注意的重點:
●調(diào)用構(gòu)造函數(shù)必須是Java中構(gòu)造函數(shù)的第一條語句。
●如果我們已經(jīng)定義了任何參數(shù)化構(gòu)造函數(shù),那么編譯器不會創(chuàng)建默認(rèn)構(gòu)造函數(shù)。反之亦然,如果我們沒有定義任何構(gòu)造函數(shù),編譯器在編譯過程中會默認(rèn)創(chuàng)建默認(rèn)構(gòu)造函數(shù)(也稱為no-arg構(gòu)造函數(shù))
●在java中,遞歸構(gòu)造函數(shù)調(diào)用無效。
構(gòu)造函數(shù)重載與方法重載
嚴(yán)格來說,構(gòu)造函數(shù)重載與方法重載有點類似。如果我們想要使用不同數(shù)量的參數(shù)來初始化一個對象,那么當(dāng)我們需要基于不同參數(shù)的方法的不同定義時,我們必須執(zhí)行構(gòu)造函數(shù)重載,因為我們會重載方法。
以上就是動力節(jié)點java培訓(xùn)機(jī)構(gòu)的小編針對“Java中的構(gòu)造函數(shù)重載教學(xué)”的內(nèi)容進(jìn)行的回答,希望對大家有所幫助,如有疑問,請在線咨詢,有專業(yè)老師隨時為你服務(wù)。
相關(guān)閱讀
初級 202925
初級 203221
初級 202629
初級 203743