我想在构造函数中使用一个对象,该对象是我的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