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){
;
}
}