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

