簡體   English   中英

如何在不同類的構造函數中使用對象?

[英]How to use an object in a constructor of a different class?

我正在嘗試在構造函數中使用一個對象,該對象是我的Date類。 我不確定,但認為我應該使用接口。

public class Date {

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

public Date(int day, int month, int year) {
    this.year = year;
    checkMonth(month);
    checkDay(day);
}

public void checkMonth(int monthIn)
{
    if(monthIn < 1 || monthIn > 12)
    {
        System.out.print("the month is invalid");
    }
    else
    {
        month = monthIn;
    }
}
public void checkDay(int dayIn)
{
    if(dayIn < 1 || dayIn > 31)
    {
        System.out.print("the day is invalid and has been set to 1");
        day = 1;
    }
    else
    {
        day = dayIn;
    }
}

public int getDay()
{
    return day;
}
public int getMonth()
{
    return month;
}
public int getYear()
{
    return year;
}

public String toString()
{
    return "birth date: " + getDay() + "/" + getMonth() + "/" + getYear();
}
}

和一個Employee類(最后一個公共void add()只是試驗和錯誤,不確定是否應該存在)

public abstract class Employee {

private String fName;
private String lName;
private int rsiNumber;
private Date DOB;

public Employee(String fName, String lName, int rsiNumber, Date DOB)
{
    this.fName = fName;
    this.lName = lName;
    this.rsiNumber = rsiNumber;
    this.DOB = DOB;
}

public String getFName()
{
    return fName;
}
public String getLanme()
{
    return lName;
}
public int getRsiNumber()
{
    return rsiNumber;
}
public Date getDOB()
{
    return DOB;
}

public void setFName(String fNameIn)
{
    fName = fNameIn;
}
public void setLName(String lNameIn)
{
    lName = lNameIn;
}
public void setRsiNumber(int rsiNumIn)
{
    rsiNumber = rsiNumIn;
}
public void setDOB(Date DOBIn)
{
    DOB = DOBIn;
}

public String toString()
{
    return null;
}
public void add(Date x)
{
    DOB  = x;
}
}

以及Employee的其他一些子類。 我試圖將Date用作Employee的構造函數中的一個對象,但是當我創建Test類時,出現一條錯誤消息,提示尚未定義構造函數。

最近幾天,我從大學生病了,不確定如何使它正常工作。

這是我的測試

public class Test {

public static void main(String [] args)
{

    Employee employees[] ={
            new Salaried("Joe", "Bloggs", "R5457998", 6, 15, 1944, 800.00 ),
            new Hourly( "Kate", "Wyse", "S6657998", 10, 29, 1960, 16.75, 40 ),
            new Commission("Jim", "Slowe", "K5655998", 9, 8, 1954, 10000, .06 )};



}
}
new Hourly( "Kate", "Wyse", "S6657998", new Date(10, 29, 1960), 16.75, 40 ),

如果您使用合成物,您的問題將得到解決。 您應該在主目錄中創建一個Date對象。

    {  Employee employees[] ={
        new Salaried("Joe", "Bloggs", "R5457998",new Date( 6, 15, 1944), 800.00 ),
        new Hourly( "Kate", "Wyse", "S6657998",,new Date (10, 29, 1960), 16.75, 40 ),
        new Commission("Jim", "Slowe", "K5655998",,new Date( 9, 8, 1954), 10000, .06 )};}

我不確定最后兩個參數是什么,但是要與現有的構造函數一起使用,您將必須執行以下操作。

public static void main(String [] args)
{

    Employee employees[] ={
        new Salaried("Joe", "Bloggs", "R5457998", new Date(6, 15, 1944)),
        new Hourly( "Kate", "Wyse", "S6657998", new Date(10, 29, 1960)),
        new Commission("Jim", "Slowe", "K5655998", new Date(9, 8, 1954))
    };

}

您應該在Employee聲明數組中執行此操作: new Salaried("Joe", "Bloggs", "R5457998", new Date(6, 15, 1944), 800.00)

您的Salaried結構應為:

public Salaried((String fName, String lName, int rsiNumber, Date DOB, float f){
    super(fname, lname, rsiNumber, dob);
    this.f=f;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM