Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.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—与多个构造函数抗争_Java_Constructor - Fatal编程技术网

Java—与多个构造函数抗争

Java—与多个构造函数抗争,java,constructor,Java,Constructor,嗨,希望有人能帮忙。我正在上一些Java课程(遗憾的是,在我们真正开始工作之前,他们不会给我们模型答案!),目前我正在尝试让多个构造函数工作。我已经有一个工作人员对象,如下所示:- public class Person { private String name; private MyDate birthday; // This is a date based on a Person's birthday private MyDate today; // A date based on t

嗨,希望有人能帮忙。我正在上一些Java课程(遗憾的是,在我们真正开始工作之前,他们不会给我们模型答案!),目前我正在尝试让多个构造函数工作。我已经有一个工作人员对象,如下所示:-

public class Person {

private String name;
private MyDate birthday; // This is a date based on a Person's birthday 
private MyDate today; // A date based on today's date
private int day; // day of birth
private int month; // month of birth
private int year; // year of birth
// private int numDays;

public Person(String name, int pp, int kk, int vv) {
    this.name = name;
    this.day = pp;
    this.month = kk;
    this.year = vv;
    this.birthday = new MyDate(pp, kk, vv); 
}
现在他们想让我们创建这样的简短版本,但这两个版本都显示了NetBeans中的错误:-

    public Person(String name) {
    MyDate today = today(); // Creates a date based on today's date
    this.Person(name, today);
}

public Person(String name, MyDate birthday) {
    this(name, birthday);
}
这是MyDate方法

public class MyDate {

private int day;
private int month;
private int year;

public MyDate(int pv, int kk, int vv) {
    this.day = pv;
    this.month = kk;
    this.year = vv;
}
有没有办法让这些工作起来? 谢谢你的关注。

你不应该离开

public Person(String name, MyDate birthday)
{
  this(name, birthday);
} 
因为它会一次又一次地调用自己:this()将调用与您传递的参数匹配的构造函数,但您向this()传递的参数与使用构造函数时相同。您正在创建一个无限循环。

您不应该离开

public Person(String name, MyDate birthday)
{
  this(name, birthday);
} 
public class Person {

    private String name;
    private MyDate birthday; // This is a date based on a Person's birthday 
    private MyDate today; // A date based on today's date

    public Person(String name, int pp, int kk, int vv) {
        this(name, new MyDate(pp, kk, vv)); 
    }

    public Person(String name, MyDate birthday) {
        this(name); //Third constructor
        this.birthday = birthday; 
    }

    public Person(String name) {
        this.name = name;
    }

}

class MyDate{
    private int day;
    private int month;
    private int year;

    public MyDate(int pv, int kk, int vv) {
        this.day = pv;
        this.month = kk;
        this.year = vv;
    }
}
因为它会一次又一次地调用自己:this()将调用与您传递的参数匹配的构造函数,但您向this()传递的参数与使用构造函数时相同。您正在创建一个无限循环。

您需要做的就是:

public Person(String name, MyDate birthday) {
this.name = name;
this.birthday = birthday;
}
public class Person {

    private String name;
    private MyDate birthday; // This is a date based on a Person's birthday 
    private MyDate today; // A date based on today's date

    public Person(String name, int pp, int kk, int vv) {
        this(name, new MyDate(pp, kk, vv)); 
    }

    public Person(String name, MyDate birthday) {
        this(name); //Third constructor
        this.birthday = birthday; 
    }

    public Person(String name) {
        this.name = name;
    }

}

class MyDate{
    private int day;
    private int month;
    private int year;

    public MyDate(int pv, int kk, int vv) {
        this.day = pv;
        this.month = kk;
        this.year = vv;
    }
}
如果类MyDate具有日期、月份和年份的getter和setter,那么构造函数也可以执行以下操作:

public Person(String name, MyDate birthday) {
this(name, birthday.getDay(), birthday.getMonth(), birthday.getYear());
}
为此,您需要更新MyDate类,如下所示:

public class MyDate {

private int day;
private int month;
private int year;

public MyDate(int pv, int kk, int vv) {
    this.day = pv;
    this.month = kk;
    this.year = vv;
}

public void setDay(int day) {
this.day = day;
}
public void setMonth(int month) {
this.day = month;
}
public void setYear(int year) {
this.day = year;
}
public int getDay() {
return this.day;
}
public int getMonth() {
return this.month;
}
public int getYear() {
return this.year;
}
}您需要做的就是:

public Person(String name, MyDate birthday) {
this.name = name;
this.birthday = birthday;
}
如果类MyDate具有日期、月份和年份的getter和setter,那么构造函数也可以执行以下操作:

public Person(String name, MyDate birthday) {
this(name, birthday.getDay(), birthday.getMonth(), birthday.getYear());
}
为此,您需要更新MyDate类,如下所示:

public class MyDate {

private int day;
private int month;
private int year;

public MyDate(int pv, int kk, int vv) {
    this.day = pv;
    this.month = kk;
    this.year = vv;
}

public void setDay(int day) {
this.day = day;
}
public void setMonth(int month) {
this.day = month;
}
public void setYear(int year) {
this.day = year;
}
public int getDay() {
return this.day;
}
public int getMonth() {
return this.month;
}
public int getYear() {
return this.year;
}

}

您应该为构造函数中的参数提供可接受的名称。 你真的需要储存日、月、年吗

public class Person {

    private String name;
    private MyDate birthday; // This is a date based on a Person's birthday 
    private MyDate today; // A date based on today's date

    public Person(String name, int day, int month, int year) {
        this(name, new MyDate(day, month, year)); 
    }

    public Person(String name) {
        this(name, today());
    }

    public Person(String name, MyDate birthday) {
        this.name = name;
        this.birthday = birthday;
    }

}

您应该为构造函数中的参数提供可接受的名称。 你真的需要储存日、月、年吗

public class Person {

    private String name;
    private MyDate birthday; // This is a date based on a Person's birthday 
    private MyDate today; // A date based on today's date

    public Person(String name, int day, int month, int year) {
        this(name, new MyDate(day, month, year)); 
    }

    public Person(String name) {
        this(name, today());
    }

    public Person(String name, MyDate birthday) {
        this.name = name;
        this.birthday = birthday;
    }

}

您可以定义任意数量的构造函数,每个构造函数具有不同的签名。构造函数只能调用一个构造函数(不应该调用自身,因为这样会创建无限循环)


您可以定义任意数量的构造函数,每个构造函数具有不同的签名。构造函数只能调用一个构造函数(不应该调用自身,因为这样会创建无限循环)



谢谢Brendan,但是我如何解构birthday,以便将其发送到原始Person对象/方法,即第一个人需要一组3个整数来构建Person可能与您在原始构造函数中构建它的方式相反哦!你的问题是你想从“MyDate”中获取信息?如果是这样,您应该调用MyDate中声明的方法来获取月份、年份等。如果MyDate是java.util.Date,这是非常明显的。你能提供更多关于MyDate的信息吗?谢谢,它们都是用'code'公共类MyDate{private int day;private int month;private int year;public MyDate(int pv,int kk,int vv){this.day=pv;this.month=kk;this.year=vv;}“代码”很好,Brendan,现在明白了,正如你说的,我只需要调用MyDate来解构它。我的感谢感谢感谢Brendan,但是我如何解构生日,以便我可以将它发送给原始的Person对象/方法,即第一个人需要一组3个整数来构建一个人可能与您在原始构造函数中构建它的方式相反哦!你的问题是你想从“MyDate”中获取信息?如果是这样,你应该调用MyDate中声明的方法来获取月份、年份等等。如果MyDate是java.util.Date,这一点很明显。你能提供更多关于MyDate的信息吗?谢谢,它们都是用“代码”公共类MyDate构建的{private int day;private int month;private int year;public MyDate(int pv,int kk,int vv){this.day=pv;this.month=kk;this.year=vv;}“code”很好的Brendan,现在知道了,正如你说的,我只需要调用MyDate来解构它。我的thanksThanks Thomas,但它们在其他地方被用于查找某人的年龄、检查谁更老等。好的,但我认为它们必须存储在MyDate对象中,而不是存储在这个对象中。然后使用today.getDay()检索这些值today.getMonth(),today.getYear()例如!干得好,托马斯,我想你已经抓住了我奋斗的核心。我现在要去做的是充满乐观的实验,而不是10分钟前的绝望。谢谢!:)很高兴听到。如果答案适合你,就接受它。祝你下一课好运!谢谢托马斯,但它们在其他方面也很常用,比如查找某人的年龄,检查谁是谁是较旧的,等等。好的,但我认为它们必须存储在MyDate对象中,而不是此对象中。然后使用today.getDay()today.getMonth(),today.getYear()检索这些值例如!干得好,托马斯,我想你已经抓住了我斗争的核心。我现在要去做的是充满乐观的实验,而不是10分钟前的绝望。谢谢!:)很高兴听到。如果答案适合你,就接受它。祝下一课好运!干得好,用户2881767和托马斯你都让我朝着正确的方向走了,我可以这样做解构我已经创造的东西。我很高兴地问,虽然我以前遇到过“能手”和“二传手”这两个词,但我从未理解它们的意义。太棒了,再次感谢。做得好,用户2881767和托马斯你们都让我朝着正确的方向前进,我可以解构我已经创造的东西。真的吗我问,虽然我以前遇到过“能手”和“二传手”这两个词,但我从未理解它们的意义。太棒了,再次感谢。