java继承当中都有一些什么样的构造函数规则?

来源:互联网 发布:天刀数据怎么导入 编辑:程序博客网 时间:2024/05/18 01:27
马克-to-win:继承当中的构造函数规则貌似复杂: 记住我给你的以下几条口诀, 你高枕无忧。1)如果你在类中(子类或父类)写了 构造函数,系统就不会再为你自动添加无参构造函数了。2)如你没有写无参构造函数, 且机器也不会为你自动添加这个无参构造函数时,你不可以主调无参构造函数。2)子类的构造函数中不能人为的写两个super。3)构造函数中要是你人工想 写super,super必须为第一句话。4)既然super必须为第一句话,创建子类对象时,构造函数调用次序为,先最低的超类直到最高的子类。

A subclass’s constructor will definitely call the default constructor(no parameter) of its superclass,unless you have something like super(1,2), even though you don't call super(),compiler will add this statement for you.
(super() must be the first statement)

例1.4.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) {
     
//        super();//注意这上下两句,只能保留一个
。。。。。。。。。。。。。。
详情请见:http://www.mark-to-win.com/JavaBeginner/JavaBeginner3_web.html#InheritanceConstrutor
0 0
原创粉丝点击