如何通过子类调用超类参数化构造函数?

var*_*run 0 java

为什么我在Employee构造函数的启动中遇到错误,找不到符号构造函数Person?

class Person {
    String name = "noname";

    Person(String nm) {
        name = nm;
    }
}

class Employee extends Person {
    String empID = "0000";

    Employee(String eid) {// error
        empID = eid;
    }
}

public class EmployeeTest {
    public static void main(String args[]) {
        Employee e1 = new Employee("4321");
        System.out.println(e1.empID);
    }
}
Run Code Online (Sandbox Code Playgroud)

Rei*_*eus 5

你需要打电话

super(name);
Run Code Online (Sandbox Code Playgroud)

作为Employee构造函数的第一个语句,因为编译器将隐式调用Person不存在的无参数构造函数

在哪里name添加参数Employee

Employee(String eid, String name) {
    super(name);
    empID = eid;
}
Run Code Online (Sandbox Code Playgroud)

查看JLS中的示例8.2-1,其中显示了在没有显式super方法调用的情况下类似示例如何失败.