首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >递归构造函数调用PersonAddress2(String,String)

递归构造函数调用PersonAddress2(String,String)
EN

Stack Overflow用户
提问于 2018-11-29 12:09:42
回答 2查看 523关注 0票数 2

在创建几个构造函数时,如:

代码语言:javascript
复制
public PersonAddress2(String newNameFirst) {
    this(newNameFirst, "Not set", "Not set", "Not set");
}

public PersonAddress2(String newNameFirst, String newNameLast) {
    this(newNameFirst, newNameLast, "Not set", "Not set");
}

public PersonAddress2(String newNameFirst, String newNameLast, String newEmail){
    this(newNameFirst, newNameLast, newEmail, "Not set");
}
public PersonAddress2(String newNameFirst, String newNameLast, String newEmail, String newTeleNumb){
    this(newNameFirst, newNameLast, newEmail, newTeleNumb);
}

我会得到这个错误“递归构造函数调用PersonAddress2(String,String)”。那是什么意思?但是当我将构造函数设置为:

代码语言:javascript
复制
public PersonAddress2(String newNameFirst) {    
    this(newNameFirst, "Not set", "Not set", "Not set");
}

public PersonAddress2(String newNameFirst, String newNameLast) {
    this(newNameFirst, newNameLast, "Not set", "Not set");
}

public PersonAddress2(String newNameFirst, String newNameLast, String newEmail) {
    this(newNameFirst, newNameLast, newEmail, "Not set");
}
public PersonAddress2(String newNameFirst, String newNameLast, String newEmail, String newTeleNumb) {
    this.nameFirst = newNameFirst;
    this.nameLast = newNameLast;
    this.eMail = newEmail;
    this.teleNumb = newTeleNumb;
}

为什么代码突然起作用了?

EN

回答 2

Stack Overflow用户

发布于 2018-11-29 12:51:56

调用this() (带有任意数量的参数)引用当前的类构造函数。因此,当我们调用this(String arg)编译器时,在当前类中查找具有一个字符串参数的构造函数。如果它不存在,你将会得到编译时错误

代码语言:javascript
复制
class Person {
     private String data;
     Person() {
         this("test");
     }
     Person(String data) {
         this.data = data;
     }
}

这可以很好地工作,但是当您删除带有字符串arg的构造函数时,它会给出编译时错误。

构造函数主要用于初始化实例变量,一个类不能有多个签名相同的构造函数。

代码语言:javascript
复制
class Person {
     private String data;
     Person(String data) {
         this("test");
     }
     Person(String data) {  // compile time error.
         this.data = data;
     }
}

这就是为什么我们定义的每个构造函数都是唯一的。

因此,在您的第一段代码中,有一个构造器执行链,从一个参数构造函数到两个参数构造函数,然后是三个参数构造函数,最后是四个参数构造函数。现在来看第四个构造函数,编写的代码是this(newNameFirst, newNameLast, newEmail, newTeleNumb);,它只是一个对第四个构造函数(带有四个字符串参数)的调用。所以它继续循环,没有结束,所以为了避免这种情况,Java将其定义为编译时错误。

现在,在您的第二段代码中,您正在使用构造函数参数对属性进行初始化或赋值,因此不会遇到无限循环,这就是您的代码将被传递的原因。

下面是您将遇到相同错误的另一种方式

代码语言:javascript
复制
class Person {
    private String data;
    public Person() {
        this("test");
    }
    public Person(String data) {
        this();
    }
}
票数 2
EN

Stack Overflow用户

发布于 2018-11-29 12:11:59

在您的第一个代码实例中,您将从其内部调用构造函数:

代码语言:javascript
复制
public PersonAddress2(String newNameFirst, String newNameLast, String newEmail, String newTeleNumb){
    this(newNameFirst, newNameLast, newEmail, newTeleNumb); // calls the same constructor, hence the error
}

从另一方面来说,上述结果会导致不确定的递归

代码语言:javascript
复制
public PersonAddress2(String newNameFirst, String newNameLast, String newEmail, String newTeleNumb) {
    this.nameFirst = newNameFirst;
    this.nameLast = newNameLast;
    this.eMail = newEmail;
    this.teleNumb = newTeleNumb;
}

断开调用构造函数的链,就像您的followed模式一样。

这里的模式是使用所有参数调用构造器,即使没有提供任何属性(只是对于这些属性,值默认为"Not set"),仅为了可读性而调整

代码语言:javascript
复制
public PersonAddress2(String newNameFirst) {
    new PersonAddress2(newNameFirst, "Not set", "Not set", "Not set"); // just a way to read
}

public PersonAddress2(String newNameFirst, String newNameLast) {
    new PersonAddress2(newNameFirst, newNameLast, "Not set", "Not set");
}

public PersonAddress2(String newNameFirst, String newNameLast, String newEmail){
   new PersonAddress2(newNameFirst, newNameLast, newEmail, "Not set");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53531698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档