0
由于存在一个错误(将name
而不是newName
传递给第二个构造函数),但我很好奇为什么下面的代码没有编译并且抱怨“在超类型构造函数之前无法引用”。由于Java在超类型构造函数之前无法引用
public class Plant {
String name;
public Plant(){
System.out.println("Constructor running");
}
public Plant(String newname) {
this(name, 7); //compiler error, cannot reference Plant.name before supertype constructor has been called
System.out.println("Constructor 2 running");
}
public Plant(String maximax, int code){
this.name = maximax;
System.out.print("Constructor 3 running");
}
private void useName(String name){
;
}
}