大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學(xué)習(xí)攻略 Java學(xué)習(xí) Java中的構(gòu)造函數(shù)重載教學(xué)

Java中的構(gòu)造函數(shù)重載教學(xué)

更新時間: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ù)重載,因為我們會重載方法。

java重載構(gòu)造方法

以上就是動力節(jié)點java培訓(xùn)機(jī)構(gòu)的小編針對“Java中的構(gòu)造函數(shù)重載教學(xué)”的內(nèi)容進(jìn)行的回答,希望對大家有所幫助,如有疑問,請在線咨詢,有專業(yè)老師隨時為你服務(wù)。

提交申請后,顧問老師會電話與您溝通安排學(xué)習(xí)

免費課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 草草精品视频 | 91av中文| 理论片毛片 | 五月天激情亚洲婷婷在线 | 日本视频中文字幕一区二区 | 欧美日韩精品一区二区在线线 | 91久久精品国产免费一区 | 久久综合视频网站 | 国产成人久久久精品一区二区三区 | 桃花综合 | 久久九九青青国产精品 | 日本1区二区三区公司 | 99re66热这里只有精品17 | 亚洲一级毛片免观看 | 精品一区二区三区在线观看视频 | 亚洲精品乱码中文字幕无线 | 久久青青草原精品国产麻豆 | 国产精品久久自在自线观看 | 四虎影院免费观看视频 | 国产一区私人高清影院 | 国语高清精品一区二区三区 | 国产精品福利资源在线 | 精品国产视频在线观看 | 精品久久亚洲一级α | 一级毛片aaa片免费观看 | 久久夜夜 | 青青青免费高清视频在线 | 爱爱小视频免费 | 大尺度视频网站久久久久久久久 | 成年人黄视频大全 | 国产精品免费久久久久影院 | 中文字幕亚洲精品久久 | 国内精品久久久久久影院老狼 | 精品玖玖| 婷婷日韩 | www亚洲精品 | 成人免费淫片在线费观看 | 国产三级观看久久 | 久久综合网久久综合 | 99久久影视| 美女视频很黄很黄又免费的 |