繁体   English   中英

Java-与多个构造函数进行斗争

[英]Java - Struggling with Multiple Constructors

您好,希望有人能提供帮助。 我正在学习一些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;
}

关于如何使它们工作的任何想法? 谢谢你的关注。

你不应该离开

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM