2016-11-14 70 views
1

我正在创建一个名为“Employee”的用户定义分类,它将保存关于员工的基本数据。如姓名,生日和雇用日期。如何用另一个参数创建一个类的对象? Java

在我以前的程序中,我创建了一个名为“Date”的用户定义类,它包含3个int值(月,日,年)。

Employee类的生日和雇用日期必须是日期类型。

这是我为我的Date类构造函数 //日,月,年进行初始化之前

public Date(int m, int d, int y){ 
    day = d; 
    month = m; 
    year = y; 
} 

和实例化一个Date对象

Date date1 = new Date(1, 1, 1582); 

这里的一个例子是我的构造函数我的员工类

public Employee(String fName, String lName, Date d1, Date d2) { 
    firstName = fName; 
    lastName = lName; 
    date1 = d1; 
    date2 = d2; 
} 

当试图创建一个对象o F中的Employee类(在另一个类,这个类被命名为 “员工测试”),我得到一个erorr

“要求:字符串,字符串,日期,日期

发现:字符串,字符串,INT, int,int,int,int,int“。

Employee e = new Employee("Tom" , "Doe",1 , 5 , 1995, 1 , 6 , 2011); 

问题似乎是该对象接收到的数据作为INT值,而不是式日期,即使日期对象需要3倍INT的值本身。

我很困惑如何创建这个对象,我的构造函数是错的吗?在创建Employee对象时,是否需要传递实际的实例化对象作为参数?

谢谢

+1

请更正您的代码片段/您使用date1和Date2声明Date对象是未声明的类。改用日期。 –

回答

10

您需要传递一个Date对象!看看这个示例:

Date date1 = new Date(1, 5, 1995); 
Date date1 = new Date(1, 6, 2011); 
Employee e = new Employee("Tom" , "Doe",date1, date2); 

现在您将一个Date对象的两个引用传递给Employee构造函数。你甚至可以这样做:

Employee e = new Employee("Tom" , "Doe", new Date(1, 5, 1995), new Date(1, 6, 2011)); 
+2

您可能会添加另一个构造函数,它直接使用'm,d,y'参数并调用现有函数来回答标题中的问题。 – schwobaseggl

0

如果你没有想创建日期之前,你还可以在呼叫建立他们:

Employee e = new Employee("Jane", "Doe", new Date(1, 4, 1993), new Date(2, 4, 1993); 
1

Answer by BrunoDM是正确的,应该被接受。

替代构造

这里有更多的代码,如由schwobaseggl的评论所说,以显示一个可选的构造以传递给Date类的另一个构造函数的参数。

请勿使用与Java捆绑在一起的类中的名称命名自己的类。所以我使用名称MiniDate而不是Date以避免与java.util.Datejava.sql.Date混淆。顺便说一句,在实际工作中,从不滚动自己的日期时间类,例如问题中看到的日期值。而是使用Java捆绑的java.time类。在此代码中,我们将使用LocalDate而不是我们自己的MiniDate类。

请参阅live code in IdeOne.com

这里是main方法显示Employee的两个不同的构造函数的每一个。首先,我们传递MiniDate的实例。在第二个例子中,我们将年月日整数传递给Employee的构造函数,然后将它们传递给MiniDate的构造函数。

late-binding features of Java根据参数的数量和数据类型自动确定实际调用哪个构造函数。

顺便说一下,在实际工作中,通过一个构造函数将年份 - 月份的各个组件传递给另一个构件将可能是一个糟糕的主意,笨拙和混乱。 Java缺少在Objective-CSwift中工作得很好的argument labels来识别一系列冗长的参数。因此,在这里最好先实例化MiniDate(或更好,LocalDate),然后将完成的对象传递给构造函数以减少总体数量参数。

/* Name of the class has to be "Main" only if the class is public. */ 
class Ideone 
{ 
    public static void main (String[] args) throws java.lang.Exception 
    { 
     MiniDate birthDate = new MiniDate(1967 , 1 , 23); 
     MiniDate hireDate = new MiniDate(2016 , 2 , 28); 
     Employee e1 = new Employee("Wendy" , "Melvoin" , birthDate , hireDate); 
     System.out.println(e1); 

     Employee e2 = new Employee("Lisa" , "Coleman" , 1968 , 2 , 24 , 2016 , 4 , 14); 
     System.out.println(e2); 
    } 
} 

这里,我们看到的构造函数,MiniDate类型的一个拍摄参数,而int文字其他服用参数。

class Employee { 
    private String firstName , lastName ; 
    private MiniDate birthDate , hireDate ; 

    // Constructor 
    public Employee(String firstNameArg , String lastNameArg , MiniDate birthDateArg , MiniDate hireDateArg) { 
     this.firstName = firstNameArg; 
     this.lastName = lastNameArg; 
     this.birthDate = birthDateArg; 
     this.hireDate = hireDateArg; 
    } 

    // Constructor 
    public Employee(String firstNameArg , String lastNameArg , int birthYearArg , int birthMonthArg , int birthDayOfMonthArg , int hireYearArg , int hireMonthArg , int hireDayOfMonthArg ) { 
     this.firstName = firstNameArg; 
     this.lastName = lastNameArg; 
     this.birthDate = new MiniDate(birthYearArg , birthMonthArg , birthDayOfMonthArg); 
     this.hireDate = new MiniDate(hireYearArg , hireMonthArg , hireDayOfMonthArg); 
    } 

    @Override 
    public String toString() { 
     String s = "Employee{ name: " + this.firstName + " " + this.lastName + " | birthDate: " + this.birthDate + " | hireDate: " + hireDate + " }" ; 
     return s; 
    } 
} 

这里是MiniDate的源代码。再次注意,在实际工作中,您将使用LocalDate而不是您自己的类,例如LocalDate birthDate = LocalDate.of(1967 , 1 , 23) ;

请注意,我始终将日期部分排序为年 - 月 - 日。这遵循ISO 8601标准的风格。我强烈建议您在日期时间工作中使用此标准的格式和样式。

class MiniDate { 
    // For teaching purposes only. In real work, use `LocalDate` class bundled with Java. 
    private int year , month , dayOfMonth ; 

    public MiniDate(int yearArg , int monthArg , int dayOfMonthArg) { 
     this.year = yearArg; 
     this.month = monthArg; 
     this.dayOfMonth = dayOfMonthArg; 
    } 

    @Override 
    public String toString() { 
     // Generate string in standard ISO 8601 format, padding with zeros as needed. 
     String s = this.year + "-" + String.format("%02d", this.month) + "-" + String.format("%02d", this.dayOfMonth) ; 
     return s ; 
    } 
} 
+0

连续6次int参数可能太多,用户无法记住。坚持ISO-8601肯定有帮助。 –

+0

@LukeLee我从来没有说过年月日的论点是一个好主意!我说我的意见是我们更直接地回答了提出的问题**,其中涉及到创建“一个类别的对象与另一个类别的论点”。 –

相关问题