gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:51:19 27 4
gpt4 key购买 nike

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

public class Foo {
private int x;

public Foo() {
this(1);
}

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

我可以以某种方式从 Foo(int x) Foo() 调用吗?

最佳答案

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

关于java从带参数的构造函数调用不带参数的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29200154/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com