更新时间:2022-12-14 19:44:15
是的,它是可能的:
public class Foo {
private int x;
public Foo() {
this(1);
}
public Foo(int x) {
this.x = x;
}
}
链接到特定的超类构造函数而不是同一个类,使用 super
而不是这个
。请注意,您只能链接到一个构造函数,它必须是构造函数体中的第一个语句。
To chain to a particular superclass constructor instead of one in the same class, use super
instead of this
. Note that you can only chain to one constructor, and it has to be the first statement in your constructor body.
另请参阅此相关问题,该问题与C#相关,但适用相同的原则。
See also this related question, which is about C# but where the same principles apply.