簡體   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