继承——构造函数的继承关系

来源:互联网 发布:淘宝宝贝详情模板下载 编辑:程序博客网 时间:2024/05/20 02:53
package b_extends_a;/** * Created by warning on 2017-08-01. * * 继承中的构造方法 1、子类的构造过程中必须调用其基类的构造方法。 2、子类可以在自己的构造方法中使用super(argument_list)调用基类的构造方法。   2.1、使用this(argument_list)调用本类的另外构造方法。     2.2、如果调用super,必须写在子类构造方法的第一行。 3、如果子类的构造方法中没有显示的调用基类的构造方法,则系统默认调用基类的无参数构造方法。 4、如果子类构造方法中既没有显示调用基类构造方法,而基类又没有无参数的构造方法,则编译出错。 */public class GouZaoHanShu {    public static void  main(String []args) {        A a = new B();        a.Adoingsomething();    }}class A{    public A(){        System.out.println("A");    }    /*    * 这是第四种情况,在没有无参构造方法的情况在,是编译错误的    * *///    public A(int a){//        System.out.println("A");//    }    public void Adoingsomething(){        System.out.println("a doingsomething");    }}class B extends A{    public B(){        System.out.println("B");    }    public void Bdoingsomething(){        System.out.println("b doingsomething");    }}
原创粉丝点击