【发布时间】:2015-12-07 15:11:06
【问题描述】:
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
super(“faculty”);
}
}
class Employee extends Person {
private String name;
public Employee() {
name = “no name”;
System.out.println("(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
name = s;
System.out.println(s);
}
}
class Person {
//What if there was a parameterized constructor here
// e.g. public Person(String s){
// ... code ...
// }
}
在上面的 Java 代码中,如果我将 Person 类留空,并在 Faculty 类的无参数构造函数中调用超级构造函数,则会调用 Employee 的构造函数。但是如果 Person 类中有一个参数化的构造函数怎么办。哪个超级构造函数会被调用?员工一号还是个人一号?
如果我不在子类中调用超级构造函数,是否仍会调用超级构造函数?
【问题讨论】:
-
为什么不试试呢?
标签: java oop inheritance constructor super