-1
看來,如果程序員提供了構造函數,並且使用對super()的調用,則不能在該類中調用其他構造函數(即,不要超載)。這是Java固有的正常行爲嗎?爲什麼?通過調用super()重載一個包含顯式構造函數的類
abstract class Four {
Four () { }
Four (int x) { }
}
class Three extends Four {
public Three (String name) { }
Three (int t, int y) { }
}
class Two extends Three {
Two () { super ("number"); }
// Two (int t, int y) { } //causes an error when uncommented
}
class One extends Two {
public static void main (String [ ] args) {
new One ();
}
}
你能解釋一下你的問題,指出你的例子中的細節嗎? – ProgrammersBlock
當超類沒有零參數構造函數時,子類構造函數必須全部明確地調用'super'。 'Three'沒有任何零參數構造函數,所以'Two'必須在其所有構造函數中調用'super'。 – 4castle
@ProgrammersBlock類2中的重載構造函數在取消註釋時會導致編譯器錯誤。 @ Jon \ Skeet提供瞭解決方案。 – Michael