继承三
- 如果父类没有完成初始化,子类将无法使用父类的数据
如果父类中没有空参构造方法,只有带参构造方法,会出现什么?
public class Person {
public Person(){
System.out.println("我是父类的空参构造方法");
}
public Person(String name,int age){
this.name = name;
this.age = age;
System.out.println("我是父类的有参构造方法");
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Student extends Person {
public Student(){
//super();
System.out.println("我是子类的空参构造方法......");
}
public Student(int score){
//super();
this.score = score;
System.out.println("我是子类的有参构造方法......");
public int getScore(){
return score;
}
public void setScore(int score){
this.score = score;
}
}
public class Test {
public static void main(String[] args) {
Student stu1 = new Student();
}
}
效果图:
注意:构造方法的第一条语句默认都是super(),子类调用父类的无参构造,二父类调用Object(继承系统中的最顶层父类)