java繼承當(dāng)中都有一些什么樣的構(gòu)造函數(shù)規(guī)則?
繼承當(dāng)中的構(gòu)造函數(shù)規(guī)則
馬克-to-win:繼承當(dāng)中的構(gòu)造函數(shù)規(guī)則貌似復(fù)雜: 記住我給你的以下幾條口訣, 你高枕無(wú)憂。1)如果你在某類中寫(xiě)了帶參構(gòu)造函數(shù),系統(tǒng)就不會(huì)再為你在那類中自動(dòng)添加無(wú)參構(gòu)造函數(shù)了。2)如你沒(méi)有寫(xiě)無(wú)參構(gòu)造函數(shù),且機(jī)器也不會(huì)為你自動(dòng)添加這個(gè)無(wú)參構(gòu)造函數(shù)時(shí)(因?yàn)槟阋呀?jīng)有帶參構(gòu)造函數(shù)了),你不可以主動(dòng)調(diào)無(wú)參構(gòu)造函數(shù)。3)子類的構(gòu)造函數(shù)中不能人為的寫(xiě)兩個(gè)super。4)構(gòu)造函數(shù)中要是你人工想寫(xiě)super,super必須為第一句話。構(gòu)造函數(shù)中要是你不寫(xiě)super,機(jī)器會(huì)為你加無(wú)參數(shù)super().馬克-to-win:5)既然super必須為第一句話,創(chuàng)建子類對(duì)象時(shí),構(gòu)造函數(shù)調(diào)用次序?yàn)椋茸畹偷某愔钡阶罡叩淖宇悺?br>馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。
例1.6.1
class AAAMark_to_win {
AAAMark_to_win() {
System.out.println("Inside AAAMark_to_win's constructor.");
}
AAAMark_to_win(int j) {
System.out.println(j);
}
}
class BBB extends AAAMark_to_win {
BBB() {
super(3);
System.out.println("Inside BBB's constructor.");
}
BBB(int i) {
System.out.println(i);
}
}
class C extends BBB {
C(int a) {
/*馬克-to-win: super必須是第一句話,徹底不寫(xiě)也行。因?yàn)闄C(jī)器會(huì)為你自動(dòng)加。
super must be the first statement. without it, also ok.the result
is exactly the same as right now, because machine will add automatically for you. but if you have it, it must be the first statement.
*/
// super();//注意這上下兩句,只能保留一個(gè)
super(1);
System.out.println("Inside C's constructor.");
}
}
public class Test {
public static void main(String args[]) {
/*馬克-to-win: 這里你不能寫(xiě)成C(),即使不談繼承,也一樣。因?yàn)槟阋呀?jīng)有C(int a) {} ,系統(tǒng)不會(huì)為你自動(dòng)添加C() {} 。
here you can not write as new C(); even without inheritance,still, you can not write as new C(); because if you have C(int a) {} already, machine will not automatically add C() {} for you.*/
C c = new C(5);
}
}
result:
Inside AAAMark_to_win's constructor.
1
Inside C's constructor.
例1.6.2
class AAAMark_to_win {
AAAMark_to_win() {
System.out.println("Inside AAAMark_to_win's constructor.");
}
}
class BBB extends AAAMark_to_win {
BBB(double g) {
System.out.println("Inside BBB's constructor.");
}
}
class C extends BBB {
C(int a) {
/* 馬克-to-win: 假如你沒(méi)有下列的super(5.9),系統(tǒng)將報(bào)錯(cuò),因?yàn)槟銢](méi)有BBB(){}, 系統(tǒng)也不會(huì)自動(dòng)替你加。if you don't have the following super(5.9), it will report error, because you don't have BBB(){}.*/
super(5.9);
System.out.println("Inside C's constructor1.");
}
}
public class Test {
public static void main(String args[]) {
C c = new C(5);
}
}
result is:
Inside AAAMark_to_win's constructor.
Inside BBB's constructor.
Inside C's constructor1.
例1.6.2-b:
class AAAMark_to_win {
AAAMark_to_win() {
System.out.println("Inside AAAMark_to_win's constructor.");
}
AAAMark_to_win(int j) {
System.out.println(j);
}
}
class BBB extends AAAMark_to_win {
}
class C extends BBB {
C(int a) {
/*馬克-to-win: super必須是第一句話,徹底不寫(xiě)也行。因?yàn)闄C(jī)器會(huì)為你自動(dòng)加。
super must be the first statement. without it, also ok.the result
is exactly the same as right now, because machine will add automatically for you. but if you have it, it must be the first statement.
*/
// super();//注意這上下兩句,只能保留一個(gè)
// super(1);
System.out.println("Inside C's constructor.");
}
}
public class Test {
public static void main(String args[]) {
/*馬克-to-win: 這里你不能寫(xiě)成C(),即使不談繼承,也一樣。因?yàn)槟阋呀?jīng)有C(int a) {} ,系統(tǒng)不會(huì)為你自動(dòng)添加C() {} 。
here you can not write as new C(); even without inheritance,still, you can not write as new C(); because if you have C(int a) {} already, machine will not automatically add C() {} for you.*/
C c = new C(5);
}
}
結(jié)果:
Inside AAAMark_to_win's constructor.
Inside C's constructor.
例1.6.3
class AAAMark_to_win {
AAAMark_to_win() {
System.out.println("Inside A's constructor.");
}
}
class BBB extends AAAMark_to_win {
BBB() {
System.out.println("Inside BBB's constructor.");
}
}
class C extends BBB {
/* 馬克-to-win: 無(wú)論C()整個(gè)comment out,還是把C()里面的內(nèi)容都去掉,我試過(guò),效果是一樣的,AAAMark_to_win和BBB的構(gòu)造函數(shù)都會(huì)被調(diào)用。 AAAMark_to_win' constructor and BBB's constructor still are called, */
C() {
System.out.println("Inside C's constructor.");
}
}
public class Test {
public static void main(String args[]) {
C c = new C();
}
}
result is:
Inside A's constructor.
Inside BCC's constructor.
Inside C's constructor.