您的位置:首页 > 编程语言 > Java开发

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

2012-07-23 12:16 344 查看
publicclass Person {
private String
name;
private Integer
age;
public Person() {
super();
this.name =
null;
this.age = -1;
}
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
}

publicclass Student
extends Person{
private String
id;
public Student() {
super();
}
public Student(String id) {
super();
this.id = id;
}
}

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

子类student不写super() jvm也会默认的调用父类的无参构造.但有一种情况不会报错.在子类student中的构造函数,显式的调用父类的有参构造函数,则父类中就不用写无参的构造函数了.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: