我想在構造函數中使用一個對象,該對象是我的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)};
}
}
日期已經是Java的一個類,請確保您指向正確的那一個。另外儘量不要用自己的自定義類隱藏本地類,用一些項目特定的前綴重命名你的類。 – AsG
確切的錯誤是什麼意思? 「Salaried」,「Hourly」和「Comission」的構造函數是什麼樣的? –
@ Sam我確切的錯誤是構造函數Salaried(字符串,字符串,字符串,int,int,int,double)未定義 @Aakash Goyal 感謝您的提示,關於類的命名,我是隻要按照我的講師的指示,我可以重新命名它,但我不認爲它是問題所在。我怎麼能確定我指着我的班? – user2938365