简体   繁体   English

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

[英]java call constructor without parameters from constructor with parameters

I have the next question. 我有下一个问题。 I have the code: 我有代码:

public class Foo {
  private int x;

  public Foo() {
    this(1);
  }

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

Can I call from Foo(int x) Foo() somehow? 我可以通过Foo(int x) Foo()打电话吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM