小知识:
- 父类的构造方法不能被继承;
- 构造方法被逐级调用
- super关键字: 调用父类构造方法 / 调用父类方法
注意:
子类构造方法中默认第一句为 super( ) : 即继承父类的无参构造方法
public A(){
super(); //系统默认调用父类构造方法
}
故父类中必须有无参数构造方法,若只有有参数构造方法,则会出现错误!
代码:
public class Faculty extends Employe{
public static void main(String[] args) {
new Faculty();
}
public Faculty(){
System.out.println("(4) Perform Faculty's task");
}
}
public class Person {
public Person(){
System.out.println("(1) Performs Person's tasks");
}
}
public class Employe extends Person{
public Employe(){
this("(2) Invoke Employe's overloaded constructor");
System.out.println("(3) Performs Employe's task");
}
public Employe(String s){
System.out.println(s);
}
}
输出结果:
(1) Performs Person's tasks
(2) Invoke Employe's overloaded constructor
(3) Performs Employe's task
(4) Perform Faculty's task