2013-10-30 32 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; 
} 
相关问题