類在創(chuàng)建時,會執(zhí)行類的成員變量的初始化和執(zhí)行類的構造方法,構造方法是JAVA在創(chuàng)建對象 new 動作時要執(zhí)行的方法
1 JAVA基本數(shù)據(jù)類型初始化默認值
int 0
long 0L
float 0L
double 0D
char ’\u0000’
boolean false
所有引用數(shù)據(jù)類型 null
2 構造方法
構造方法 examda提示: 是類在創(chuàng)建實例時 new 動作時要執(zhí)行的方法, examda提示: 如果類中沒有定義構造方法 JAVA編譯器會為類自動添加一個構造方法
ClassName(){}
構造方法的名稱必須與類名一樣,而且構造方法沒有返回值;
當類中已經(jīng)創(chuàng)建了構造方法,編譯器就不再為類自動創(chuàng)建構造方法;
所以當創(chuàng)建一個帶參數(shù)的構造方法后,必須同時要創(chuàng)建一個不帶參數(shù)的構造方法
example 1
public class Example{
int id;
int age;
public Example(){
}
public Example(int id,int age){
this.id=id;
this.age=age;
}
}
1 JAVA基本數(shù)據(jù)類型初始化默認值
int 0
long 0L
float 0L
double 0D
char ’\u0000’
boolean false
所有引用數(shù)據(jù)類型 null
2 構造方法
構造方法 examda提示: 是類在創(chuàng)建實例時 new 動作時要執(zhí)行的方法, examda提示: 如果類中沒有定義構造方法 JAVA編譯器會為類自動添加一個構造方法
ClassName(){}
構造方法的名稱必須與類名一樣,而且構造方法沒有返回值;
當類中已經(jīng)創(chuàng)建了構造方法,編譯器就不再為類自動創(chuàng)建構造方法;
所以當創(chuàng)建一個帶參數(shù)的構造方法后,必須同時要創(chuàng)建一個不帶參數(shù)的構造方法
example 1
public class Example{
int id;
int age;
public Example(){
}
public Example(int id,int age){
this.id=id;
this.age=age;
}
}