我有以下代码
public class Employee {
private String name;
private String gender;
private int age;
final String DEFAULT_GENDER = "male";
final int DEFAULT_AGE = 18;
public Employee(String name,String gender,int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public Employee(String name) {
this(name,DEFAULT_GENDER,DEFAULT_AGE);
}
}
我收到以下错误
Cannot reference Employee.DEFAULT_GENDER before supertype constructor has been called
我不明白为什么这么说Employee.DEFAULT_GENDER
?我还没有将其定义为静态!为什么不允许我用 3 个参数调用构造函数?我已经定义DEFAULT_GENDER
并DEFAULT_AGE
确保了一些默认值。创建 Employee 对象所需的只是他的姓名(在这种情况下,性别和年龄设置为默认值。也不允许使用默认构造函数)。关于为什么会发生这种情况的任何观点?