-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