2013-10-30 134 views
0

我想在構造函數中使用一個對象,該對象是我的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(); 
} 
} 

與勞動者類(公共無效的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; 
} 
} 

隨着員工的其他一些子類。 我想使用Date作爲Employee的構造函數中的對象,但是當我創建我的Test類時,出現錯誤,提示構造函數尚未定義。

最近幾天我從大學生病了,我不知道如何得到這個工作。

這是我的測試,如果u使用組成

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



} 
} 
+0

日期已經是Java的一個類,請確保您指向正確的那一個。另外儘量不要用自己的自定義類隱藏本地類,用一些項目特定的前綴重命名你的類。 – AsG

+0

確切的錯誤是什麼意思? 「Salaried」,「Hourly」和「Comission」的構造函數是什麼樣的? –

+0

@ Sam我確切的錯誤是構造函數Salaried(字符串,字符串,字符串,int,int,int,double)未定義 @Aakash Goyal 感謝您的提示,關於類的命名,我是隻要按照我的講師的指示,我可以重新命名它,但我不認爲它是問題所在。我怎麼能確定我指着我的班? – user2938365

回答

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

我被授予數組使用它的方式,有沒有其他方式來做到這一點? – user2938365

+0

用超級構造函數播放參數 – iluxa

0

您的問題將得到解決。你應該在主體中創建一個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)};} 
0

我不知道什麼是最後兩個論點,但與現有的建設者的工作,你將不得不做一些像下面這樣。

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

} 
0

你應該這樣做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; 
} 
相關問題