java中的構造方法是一種特殊類型的方法,用于初始化對象。Java構造函數(shù)在對象創(chuàng)建時被調用。 它構造值,即提供對象的數(shù)據(jù),這是為什么它被稱為構造函數(shù)。
構造函數(shù)基本上定義了兩個規(guī)則。它們分別如下 -
有兩種類型的構造函數(shù):

沒有參數(shù)的構造函數(shù)稱為默認構造函數(shù)。默認構造函數(shù)的語法如下:
<class_name>(){}
默認構造函數(shù)的示例:
在這個例子中,在Bike類中創(chuàng)建了無參數(shù)(no-arg)構造函數(shù)。它將在對象創(chuàng)建時被調用。
class Bike1 {
Bike1() {
System.out.println("Bike is created");
}
public static void main(String args[]) {
Bike1 b = new Bike1();
}
}
上面的示例代碼運行結果如下 -
Bike is created
規(guī)則:如果類中沒有構造函數(shù),編譯器會自動創(chuàng)建一個默認構造函數(shù)。
問題: 默認構造函數(shù)的目的是什么?
默認構造函數(shù)根據(jù)類型為對象提供默認值,如:0,null等。
顯示默認值的默認構造函數(shù)示例
class Student3 {
int id;
String name;
void display() {
System.out.println(id + " " + name);
}
public static void main(String args[]) {
Student3 s1 = new Student3();
Student3 s2 = new Student3();
s1.display();
s2.display();
}
}
運行上面代碼,得到如下結果 -
0 null
0 null
解釋: 在上面的類中,代碼中并沒有創(chuàng)建任何構造函數(shù),但編譯器自動提供了一個默認構造函數(shù)。默認構造函數(shù)分別為字段:id 和 name 分別提供了0和null值。
具有參數(shù)的構造函數(shù)稱為參數(shù)化構造函數(shù)。
問題: 為什么使用參數(shù)化構造函數(shù)?
回答: 參數(shù)化構造函數(shù)用于為不同對象提供不同初始化的值。
參數(shù)化構造函數(shù)的示例
在這個例子中,我們創(chuàng)建了具有兩個參數(shù)的Student類的構造函數(shù)。構造函數(shù)中柯有任意數(shù)量的參數(shù)。
class Student4 {
int id;
String name;
Student4(int i, String n) {
id = i;
name = n;
}
void display() {
System.out.println(id + " " + name);
}
public static void main(String args[]) {
Student4 s1 = new Student4(111, "Karan");
Student4 s2 = new Student4(222, "Aryan");
s1.display();
s2.display();
}
}
運行上面代碼,得到如下結果 -
111 Karan
222 Aryan
構造方法重載是Java中的一種技術,一個類可以有任何數(shù)量的參數(shù)列表不同的構造函數(shù)。編譯器通過構造函數(shù)參數(shù)列表中的參數(shù)數(shù)量及其類型來區(qū)分這些構造函數(shù)。
構造函數(shù)重載示例
class Student5 {
int id;
String name;
int age;
Student5(int i, String n) {
id = i;
name = n;
}
Student5(int i, String n, int a) {
id = i;
name = n;
age = a;
}
void display() {
System.out.println(id + " " + name + " " + age);
}
public static void main(String args[]) {
Student5 s1 = new Student5(111, "Karan");
Student5 s2 = new Student5(222, "Aryan", 25);
s1.display();
s2.display();
}
}
上面示例代碼,執(zhí)行后輸出結果如下 -
111 Karan 0
222 Aryan 25
構造函數(shù)和方法之間有許多區(qū)別,它們如下面列出 -
| Java構造函數(shù) | Java方法 |
|---|---|
| 構造器用于初始化對象的狀態(tài)(數(shù)據(jù))。 | 方法用于暴露對象的行為。 |
| 構造函數(shù)不能有返回類型。 | 方法一般都有返回類型。 |
| 構造函數(shù)隱式調用。 | 方法要顯式調用。 |
| 如果沒有指定任何構造函數(shù),java編譯器提供一個默認構造函數(shù)。 | 在任何情況下編譯器都不會提供默認的方法調用。 |
| 構造函數(shù)名稱必須與類名稱相同。 | 方法名稱可以或可以不與類名稱相同(隨意)。 |
在Java中沒有復制構造函數(shù)。但是可以將一個對象的值復制到另一個中,就像C++中的復制構造函數(shù)。
在java中有很多方法可以將一個對象的值復制到另一個對象中。它們分別是:
clone()方法在這個例子中,使用java構造函數(shù)將一個對象的值復制到另一個對象中。
class Student6 {
int id;
String name;
Student6(int i, String n) {
id = i;
name = n;
}
Student6(Student6 s) {
id = s.id;
name = s.name;
}
void display() {
System.out.println(id + " " + name);
}
public static void main(String args[]) {
Student6 s1 = new Student6(111, "Karan");
Student6 s2 = new Student6(s1);
s1.display();
s2.display();
}
}
上面示例代碼,執(zhí)行后輸出結果如下 -
111 Karan
111 Karan
不使用構造函數(shù)復制值
可以通過將對象值分配給另一個對象,將一個對象的值復制到另一個對象中。 在這種情況下,不需要創(chuàng)建構造函數(shù)。
class Student7 {
int id;
String name;
Student7(int i, String n) {
id = i;
name = n;
}
Student7() {
}
void display() {
System.out.println(id + " " + name);
}
public static void main(String args[]) {
Student7 s1 = new Student7(111, "Karan");
Student7 s2 = new Student7();
s2.id = s1.id;
s2.name = s1.name;
s1.display();
s2.display();
}
}
上面示例代碼,執(zhí)行后輸出結果如下 -
111 Karan
111 Karan
問題1: 構造函數(shù)有返回值 ?
回答: 是的,構造函數(shù)返回當前類的實例(不能指定返回值類型,但它會返回一個值)。
問題2: 可以構造函數(shù)執(zhí)行其他任務而不是初始化?
回答: 是的,比如:對象創(chuàng)建,啟動線程,調用方法等。你可以像在方法中執(zhí)行的任何操作一樣,在構造函數(shù)中也可以做到這些。