类之间的关系 从底层的构造器返回到最初始的constructors
然后再从最高级往下依次进行初始化:
三个原则:
(1) if you donnt have a superclass, the compiler gonna give you once.
(2) If u donnot have a constructor, java will give you once.
(3) 1st line in constructor must be this or super to call different constructors(a class can have mutiple constructors)
otherwise: super() : 即如果什么都没有,就自动插入这个super();
Challenge:
public class Person(){
private String name;
public Person (String n){
super();
this.name = n;
}
public void setName(){
this.name = n;
}
}
public class Student extends Person(){
public Student(){
this.setName("Student")
}
}
compiler error:
构造器的第一行一定要调用同类或者超类的构造器,这里没有就默认super();
回溯到超类中: 没有无参数构造器: compiler error