java中的子类和父类问题(补充)

来源:互联网 发布:从godaddy转出域名 编辑:程序博客网 时间:2024/06/05 09:45

publicclass Person {

    private Stringname;

    private Integerage;

    public Person() {

       super();

       this.name =null;

       this.age = -1;

    }

    public Person(String name, Integer age) {

       super();

       this.name = name;

       this.age = age;

    }

}

publicclass Studentextends Person{

    private Stringid;

    public Student() {

       super();

    }

    public Student(String id) {

       super();

       this.id = id;

    }

}

//如果将person()的无参构造方法删掉.则会报习异常.是因为在子类student中super() --->就是掉用的是父类person的无参构造方法.

子类student不写super() jvm也会默认的调用父类的无参构造.但有一种情况不会报错.在子类student中的构造函数,显式的调用父类的有参构造函数,则父类中就不用写无参的构造函数了.


原创粉丝点击