0

我试图在构造函数中使用一个对象,该对象是我的日期类。我不确定,但认为我应该使用界面。

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 类(最后的 public 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 的其他一些子类。我试图在 Employee 的构造函数中使用 Date 作为对象,但是当我创建我的 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 )};



}
}
4

4 回答 4

0
new Hourly( "Kate", "Wyse", "S6657998", new Date(10, 29, 1960), 16.75, 40 ),
于 2013-10-30T17:34:03.987 回答
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 )};}
于 2013-10-30T17:37:06.483 回答
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))
    };

}
于 2013-10-30T17:40:28.230 回答
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;
}
于 2013-10-30T17:40:54.903 回答