在本教程中,您將在示例的幫助下了解Java構(gòu)造函數(shù),如何創(chuàng)建和使用它們以及不同類型的構(gòu)造函數(shù)。
在Java中,每個(gè)類都有它的構(gòu)造函數(shù),當(dāng)類的對(duì)象被創(chuàng)建時(shí),該構(gòu)造函數(shù)將被自動(dòng)調(diào)用。構(gòu)造函數(shù)類似于方法,但實(shí)際上它不是方法。
一個(gè)Java方法和Java構(gòu)造函數(shù)以通過(guò)其名稱和返回類型進(jìn)行區(qū)分。構(gòu)造函數(shù)與類的構(gòu)造函數(shù)同名,并且不返回任何值。例如,
class Test { Test() { //構(gòu)造函數(shù)體 } }
在這里,Test()是一個(gè)構(gòu)造函數(shù)。它具有與該類相同的名稱,并且沒(méi)有返回類型。
class Test { void Test() { // 方法主體 } }
這里,Test()與類的名稱相同。但是,它有一個(gè)返回類型void。因此,它是一個(gè)方法,而不是一個(gè)構(gòu)造函數(shù)。
class Main { private int x; // 構(gòu)造函數(shù)體 private Main(){ System.out.println("構(gòu)造函數(shù)被調(diào)用"); x = 5; } public static void main(String[] args){ //創(chuàng)建對(duì)象時(shí)調(diào)用構(gòu)造函數(shù) Main obj = new Main(); System.out.println("x 的值 = " + obj.x); } }
輸出:
構(gòu)造函數(shù)被調(diào)用 x 的值 = 5
在上面的示例中,我們有一個(gè)名為Main()的私有構(gòu)造函數(shù)。在main方法中,我們正在創(chuàng)建一個(gè)名為obj的類對(duì)象。
Main obj = new Main();
在此過(guò)程中,將調(diào)用構(gòu)造函數(shù)。因此,執(zhí)行print語(yǔ)句并初始化變量x。
在Java中,構(gòu)造函數(shù)可以分為3種類型:
無(wú)參數(shù)構(gòu)造函數(shù)
默認(rèn)構(gòu)造函數(shù)
參數(shù)構(gòu)造函數(shù)
Java構(gòu)造函數(shù)可以具有或可以不具有任何參數(shù)。如果構(gòu)造函數(shù)不接受任何參數(shù),則稱為無(wú)參數(shù)構(gòu)造函數(shù)。例如,
private Constructor() { // 構(gòu)造函數(shù)體 }
class Main { int i; //沒(méi)有參數(shù)的構(gòu)造函數(shù) private Main(){ i = 5; System.out.println("Object created and i = " + i); } public static void main(String[] args) { //不帶任何參數(shù)調(diào)用構(gòu)造函數(shù) Main obj = new Main(); } }
輸出:
Object created and i = 5
在這里,構(gòu)造Main()函數(shù)不接受任何參數(shù)。
您是否注意到Main()構(gòu)造函數(shù)的訪問(wèn)修飾符是私有(private)的?
這是因?yàn)樵搶?duì)象是從同一類中實(shí)例化的。因此,它可以訪問(wèn)構(gòu)造函數(shù)。
但是,如果對(duì)象是在類外部創(chuàng)建的,則必須聲明構(gòu)造函數(shù)為public才能訪問(wèn)它。例如:
class Company { String domainName; // 公共構(gòu)造函數(shù) public Company(){ domainName = "(cainiaoplus.com)"; } } public class Main { public static void main(String[] args) { // 在另一個(gè)類中創(chuàng)建對(duì)象 Company companyObj = new Company(); System.out.println("Domain name = "+ companyObj.domainName); } }
輸出:
Domain name = (cainiaoplus.com)
相關(guān)閱讀: Java訪問(wèn)修飾符
如果不創(chuàng)建任何構(gòu)造函數(shù),則Java編譯器將在運(yùn)行時(shí)自動(dòng)創(chuàng)建無(wú)參數(shù)的構(gòu)造函數(shù)。此構(gòu)造函數(shù)稱為默認(rèn)構(gòu)造函數(shù)。默認(rèn)構(gòu)造函數(shù)使用默認(rèn)值初始化所有未初始化的實(shí)例變量。
類型 | 默認(rèn)值 |
---|---|
boolean | false |
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
char | \u0000 |
float | 0.0f |
double | 0.0d |
object | null |
class DefaultConstructor { int a; boolean b; public static void main(String[] args) { //調(diào)用默認(rèn)構(gòu)造函數(shù) DefaultConstructor obj = new DefaultConstructor(); System.out.println("a = " + obj.a); System.out.println("b = " + obj.b); } }
輸出:
a = 0 b = false
在上面的程序中,我們還沒(méi)有初始化變量a和b的值。但是,當(dāng)我們創(chuàng)建類的對(duì)象時(shí),我們可以在輸出中看到這些值已使用某些值進(jìn)行了初始化。
這是因?yàn)镴ava編譯器自動(dòng)創(chuàng)建了一個(gè)默認(rèn)構(gòu)造函數(shù)。構(gòu)造函數(shù)將使用默認(rèn)值0和false初始化變量a和b的值。
上面的程序等同于:
class DefaultConstructor { int a; boolean b; //私有構(gòu)造函數(shù) private DefaultConstructor() { a = 0; b = false; } public static void main(String[] args) { //調(diào)用構(gòu)造函數(shù) DefaultConstructor obj = new DefaultConstructor(); System.out.println("a = " + obj.a); System.out.println("b = " + obj.b); } }
輸出:
a = 0 b = false
與方法類似,我們可以將參數(shù)傳遞給構(gòu)造函數(shù)。此類構(gòu)造函數(shù)稱為參數(shù)化構(gòu)造函數(shù)。例如,
private Constructor (arg1, arg2, ..., argn) { // 構(gòu)造函數(shù)體 }
class Vehicle { int wheels; //接受單個(gè)值的構(gòu)造函數(shù) private Vehicle(int wheels){ this.wheels = wheels; System.out.println(wheels + " wheeler vehicle created."); } public static void main(String[] args) { //通過(guò)傳遞單個(gè)值來(lái)調(diào)用構(gòu)造函數(shù) Vehicle v1 = new Vehicle(2); Vehicle v2 = new Vehicle(3); Vehicle v3 = new Vehicle(4); } }
輸出:
2 wheeler vehicle created. 3 wheeler vehicle created. 4 wheeler vehicle created.
在上面的示例中,我們有一個(gè)名為Vehicle()的構(gòu)造函數(shù)。構(gòu)造函數(shù)接受一個(gè)名為wheels的參數(shù)。
在這里,當(dāng)創(chuàng)建對(duì)象時(shí),我們將參數(shù)傳遞給構(gòu)造函數(shù)。并且,基于參數(shù),它正在生成輸出。
與方法重載類似,我們也可以在Java中重載構(gòu)造函數(shù)。如果您不熟悉方法重載,請(qǐng)?jiān)L問(wèn)Java方法重載。
在構(gòu)造函數(shù)重載中,有兩個(gè)或多個(gè)具有不同參數(shù)的構(gòu)造函數(shù)。例如,
class Company { String domainName; //沒(méi)有參數(shù)的構(gòu)造函數(shù) public Company(){ this.domainName = "default"; } //具有單個(gè)參數(shù)的構(gòu)造函數(shù) public Company(String domainName){ this.domainName = domainName; } public void getName(){ System.out.println(this.domainName); } public static void main(String[] args) { //不帶參數(shù)調(diào)用構(gòu)造函數(shù) Company defaultObj = new Company(); //使用單個(gè)參數(shù)調(diào)用構(gòu)造函數(shù) Company nhoooObj = new Company("(cainiaoplus.com)"); defaultObj.getName(); nhoooObj.getName(); } }
輸出:
default (cainiaoplus.com)
在上面的示例中,我們有兩個(gè)構(gòu)造函數(shù):public Company()和public Company(String domainName)。
在這里,兩個(gè)構(gòu)造函數(shù)都用不同的值初始化變量domainName。因此,根據(jù)我們需要的值,我們可以從main()方法調(diào)用構(gòu)造函數(shù)。
注意,我們使用this關(guān)鍵字來(lái)指定類的變量。要了解有關(guān)this關(guān)鍵字的更多信息,請(qǐng)?jiān)L問(wèn)Java this關(guān)鍵字。
構(gòu)造函數(shù)在實(shí)例化對(duì)象時(shí)被隱式調(diào)用。
創(chuàng)建構(gòu)造函數(shù)的兩個(gè)規(guī)則是:
構(gòu)造函數(shù)的名稱應(yīng)與類的名稱相同。
Java構(gòu)造函數(shù)不得具有返回類型。
如果類沒(méi)有構(gòu)造函數(shù),則Java編譯器會(huì)在運(yùn)行時(shí)自動(dòng)創(chuàng)建默認(rèn)構(gòu)造函數(shù)。默認(rèn)構(gòu)造函數(shù)使用默認(rèn)值初始化實(shí)例變量。例如,int變量將被初始化為0
構(gòu)造函數(shù)類型:
無(wú)參數(shù)構(gòu)造函數(shù) - 不接受任何參數(shù)的構(gòu)造函數(shù)
默認(rèn)構(gòu)造函數(shù) - 如果沒(méi)有顯式定義,Java編譯器會(huì)自動(dòng)創(chuàng)建一個(gè)構(gòu)造函數(shù)。
參數(shù)化構(gòu)造函數(shù) - 接受參數(shù)的構(gòu)造函數(shù)
構(gòu)造函數(shù)不能是抽象的abstract 、static或final。
構(gòu)造函數(shù)可以重載,但不能被重寫。