Java - 与多个构造函数一起苦苦挣扎

时间:2014-04-09 14:53:55

标签: java constructor

嗨,希望有人可以提供帮助。我做了一些Java课程(遗憾的是他们没有给我们模型答案,直到我们真正得到整个工作!)并且我正在尝试让多个构造函数工作。我已经有一个工作的Person对象如下: -

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);
}

Ande这里是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;
}

关于如何让这些工作的任何想法? 谢谢你的兴趣。

4 个答案:

答案 0 :(得分:2)

你不应该离开

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

因为它会一次又一次地调用自己:this()将调用与你传递的params相匹配的构造函数,但是你将相同的params传递给this(),而不是你使用构造函数。你正在创造一个无限循环。

答案 1 :(得分:2)

您应该在构造函数中为参数指定可接受的名称。 你真的需要存储日,月和年吗?

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;
    }

}

答案 2 :(得分:1)

您需要做的就是:

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;
}

}

答案 3 :(得分:0)

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

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;
    }
}