java繼承當中都有一些什么樣的構造函數規(guī)則?

繼承當中的構造函數規(guī)則
馬克-to-win:繼承當中的構造函數規(guī)則貌似復雜: 記住我給你的以下幾條口訣, 你高枕無憂。1)如果你在某類中寫了帶參構造函數,系統(tǒng)就不會再為你在那類中自動添加無參構造函數了。2)如你沒有寫無參構造函數,且機器也不會為你自動添加這個無參構造函數時(因為你已經有帶參構造函數了),你不可以主動調無參構造函數。3)子類的構造函數中不能人為的寫兩個super。4)構造函數中要是你人工想寫super,super必須為第一句話。構造函數中要是你不寫super,機器會為你加無參數super().馬克-to-win:5)既然super必須為第一句話,創(chuàng)建子類對象時,構造函數調用次序為,先最低的超類直到最高的子類。
馬克- to-win:馬克 java社區(qū):防盜版實名手機尾號: 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必須是第一句話,徹底不寫也行。因為機器會為你自動加。
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();//注意這上下兩句,只能保留一個
        super(1);
        System.out.println("Inside C's constructor.");
    }
}

public class Test {
    public static void main(String args[]) {
/*馬克-to-win: 這里你不能寫成C(),即使不談繼承,也一樣。因為你已經有C(int a) {} ,系統(tǒ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: 假如你沒有下列的super(5.9),系統(tǒng)將報錯,因為你沒有BBB(){}, 系統(tǒ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必須是第一句話,徹底不寫也行。因為機器會為你自動加。
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();//注意這上下兩句,只能保留一個
        //    super(1);
        System.out.println("Inside C's constructor.");
    }
}

public class Test {
    public static void main(String args[]) {
/*馬克-to-win: 這里你不能寫成C(),即使不談繼承,也一樣。因為你已經有C(int a) {} ,系統(tǒ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);
    }
}


結果:
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: 無論C()整個comment out,還是把C()里面的內容都去掉,我試過,效果是一樣的,AAAMark_to_win和BBB的構造函數都會被調用。 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.