java 从带参数的构造函数调用不带参数的构造函数

java call constructor without parameters from constructor with parameters

我有下一个问题。我有代码:

public class Foo {
  private int x;

  public Foo() {
    this(1);
  }

  public Foo(int x) {
    this.x = x;
  }
}

我可以从 Foo(int x) Foo() 打电话吗?

你不能 - 这样做(调用 this())会在构造函数之间创建循环依赖,这在编译时是不允许的