在创建几个构造函数时,如:
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)”。那是什么意思?但是当我将构造函数设置为:
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;
}为什么代码突然起作用了?
发布于 2018-11-29 12:51:56
调用this() (带有任意数量的参数)引用当前的类构造函数。因此,当我们调用this(String arg)编译器时,在当前类中查找具有一个字符串参数的构造函数。如果它不存在,你将会得到编译时错误
class Person {
private String data;
Person() {
this("test");
}
Person(String data) {
this.data = data;
}
}这可以很好地工作,但是当您删除带有字符串arg的构造函数时,它会给出编译时错误。
构造函数主要用于初始化实例变量,一个类不能有多个签名相同的构造函数。
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将其定义为编译时错误。
现在,在您的第二段代码中,您正在使用构造函数参数对属性进行初始化或赋值,因此不会遇到无限循环,这就是您的代码将被传递的原因。
下面是您将遇到相同错误的另一种方式
class Person {
private String data;
public Person() {
this("test");
}
public Person(String data) {
this();
}
}发布于 2018-11-29 12:11:59
在您的第一个代码实例中,您将从其内部调用构造函数:
public PersonAddress2(String newNameFirst, String newNameLast, String newEmail, String newTeleNumb){
this(newNameFirst, newNameLast, newEmail, newTeleNumb); // calls the same constructor, hence the error
}从另一方面来说,上述结果会导致不确定的递归
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"),仅为了可读性而调整:
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");
}https://stackoverflow.com/questions/53531698
复制相似问题