Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中使用this()是否会创建更多对象的递归实例?_Java_Recursion_Constructor_This - Fatal编程技术网

在Java中使用this()是否会创建更多对象的递归实例?

在Java中使用this()是否会创建更多对象的递归实例?,java,recursion,constructor,this,Java,Recursion,Constructor,This,如果我有一个构造函数 public class Sample { public static StackOverflowQuestion puzzled; public static void main(String[] args) { puzzled = new StackOverflowQuestion(4); } } 在我的一个程序的主要方法中 public class StackOverflowQuestion { public

如果我有一个构造函数

public class Sample {


    public static StackOverflowQuestion puzzled;
    public static void main(String[] args) {

        puzzled = new StackOverflowQuestion(4);
    }
}
在我的一个程序的主要方法中

public class StackOverflowQuestion {

    public StackOverflowQuestion(){
    //does code
    }

    public StackOverflowQuestion(int a){
    this();
   }
}
这是通过构造函数2创建StackOverflowQuestion的实例,然后通过构造函数1创建StackOverflowQuestion的另一个实例,因此我现在有两个StackOverflowQuestion实例直接在彼此内部


或者constructor2在这种情况下是横向调整,然后通过constructor1创建StackOverflowQuestion的实例吗?

它只创建一个实例。它的一个用例是为构造函数参数提供默认值:

public class StackOverflowQuestion
{
    public StackOverflowQuestion(int a) {
        /* initialize something using a */
    }

    public StackOverflowQuestion() {
        this(10); // Default: a = 10
    }
}

它只创建一个实例。它的一个用例是为构造函数参数提供默认值:

public class StackOverflowQuestion
{
    public StackOverflowQuestion(int a) {
        /* initialize something using a */
    }

    public StackOverflowQuestion() {
        this(10); // Default: a = 10
    }
}
我想你的意思是:

public class StackOverflowQuestion
{

    public StackOverflowQuestion(){ // constructor
       //does code
    }

    public StackOverflowQuestion(int a){ // another constructor
       this();
    }
}
把它叫做:

StackOverflowQuestion puzzled = new StackOverflowQuestion(4);
这将只创建一个对象,因为
new
只执行一次。调用
this()
将在另一个构造函数中执行代码,而不创建新对象。该构造函数中的代码能够修改当前创建的实例。

我想您的意思是:

public class StackOverflowQuestion
{

    public StackOverflowQuestion(){ // constructor
       //does code
    }

    public StackOverflowQuestion(int a){ // another constructor
       this();
    }
}
把它叫做:

StackOverflowQuestion puzzled = new StackOverflowQuestion(4);

这将只创建一个对象,因为
new
只执行一次。调用
this()
将在另一个构造函数中执行代码,而不创建新对象。该构造函数中的代码可以修改当前创建的实例。

构造函数不创建对象。它只是初始化对象的状态。创建对象的是
new
操作符。通读一遍。这意味着什么:

public class StackOverflowQuestion
{

   public StackOverflowQuestion(){ // constructor
      //does code
  }

  public StackOverflowQuestion(int a){ // another constructor
     this();
  }
}

 StackOverflowQuestion puzzled = new StackOverflowQuestion(4);

new
操作符创建了一个新的
StackOverflowQuestion
对象,就在对新创建对象的引用作为结果返回并分配给
StackOverflowQuestion
引用变量之前,构造函数
StackOverflowQuestion(int A)
调用
this()
public StackOverflowQuestion()
,默认构造函数中的代码(如果有)运行,控件返回到
`StackOverflowQuestion(int a)
,其中剩余的代码(如果有)被处理以初始化新对象。

构造函数
不创建对象。它只是初始化对象的状态。创建对象的是
new
操作符。通读一遍。这意味着什么:

public class StackOverflowQuestion
{

   public StackOverflowQuestion(){ // constructor
      //does code
  }

  public StackOverflowQuestion(int a){ // another constructor
     this();
  }
}

 StackOverflowQuestion puzzled = new StackOverflowQuestion(4);

new
操作符创建了一个新的
StackOverflowQuestion
对象,就在对新创建对象的引用作为结果返回并分配给
StackOverflowQuestion
引用变量之前,构造函数
StackOverflowQuestion(int A)
调用
this()
public StackOverflowQuestion()
,默认构造函数中的代码(如果有)运行,控件返回到
`StackOverflowQuestion(int a)
,其中剩余的代码(如果有)被处理以初始化新对象。

在使用“new”操作符时创建类的实例。创建一个没有构造函数的类是完全可能的,因为在这种情况下,默认情况下,没有参数的构造函数是可用的


“this”关键字只指向这个特定对象,因此调用“this()”意味着“调用我自己的构造函数,即不带参数的构造函数并执行其中的内容”

类的实例是在使用“new”操作符时创建的。创建一个没有构造函数的类是完全可能的,因为在这种情况下,默认情况下,没有参数的构造函数是可用的

“this”关键字只是指向这个特定的对象,所以调用“this()”意味着“调用我自己的构造函数,一个没有参数的构造函数并执行里面的内容”

this()
new StackOverflowQuestion()不同

这(5)
新堆栈溢出问题(5)

this()
this(5)
调用同一类中的另一个构造函数

因此,在本例中:

public class StackOverflowQuestion
{
    private int x;
    private int y;
    private int a;

    public StackOverflowQuestion(){
       this.x = 1;
       this.y = 2;
    }

    public StackOverflowQuestion(int a){
       this();
       this.a = a;
    }
}
调用
this()
只会初始化对象,而不会创建新实例。请记住,
new StackOverflowQuestion(5)
已经被调用,调用的构造函数实际上创建了
StackOverflowQuestion
对象的一个新实例

this()
new StackOverflowQuestion()不同

这(5)
新堆栈溢出问题(5)

this()
this(5)
调用同一类中的另一个构造函数

因此,在本例中:

public class StackOverflowQuestion
{
    private int x;
    private int y;
    private int a;

    public StackOverflowQuestion(){
       this.x = 1;
       this.y = 2;
    }

    public StackOverflowQuestion(int a){
       this();
       this.a = a;
    }
}

调用
this()
只会初始化对象,而不会创建新实例。请记住,
new StackOverflowQuestion(5)
已被调用,调用的构造函数实际上创建了
StackOverflowQuestion
对象的新实例,请发布一篇演示您的问题的文章。你在这里所拥有的不会被编译…@jlordo他发布的内容没有问题。如果将
constructor
constructor2
替换为
StackOverflowQuestion
它将编译。取而代之的是,称它们为构造函数只是为了清楚起见。能够执行这一点是没有必要的,因为这只是一个关于构造函数如何工作的问题。请发布一个演示您的问题/问题的帖子。你在这里所拥有的不会被编译…@jlordo他发布的内容没有问题。如果将
constructor
constructor2
替换为
StackOverflowQuestion
它将编译。取而代之的是,称它们为构造函数只是为了清楚起见。没有必要执行这个,因为这只是一个关于构造函数如何工作的问题;不创建任何广告