如何使用另一个类的参数创建一个类的对象? Java的

时间:2016-11-14 01:08:40

标签: java date

我正在创建一个名为" Employee"它将保存有关员工的基本数据。如姓名,生日和雇用日期。

在我之前的程序中,我创建了一个名为" 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);

这是我的Employee类的构造函数

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

当尝试创建Employee类的对象时(在另一个类中,此类名为" Employee Test")我得到了一个erorr

  

"必需:字符串,字符串,日期,日期

     

found:String,String,int,int,int,int,int,int"。

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

问题似乎是对象正在以int值接收数据而不是类型Date,即使Date对象本身需要3个int值。

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

由于

3 个答案:

答案 0 :(得分: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));

答案 1 :(得分:1)

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

替代构造函数

这是附加的代码,正如schwobaseggl的注释中所建议的那样,它显示了一个替代构造函数,它接受传递给日期类的另一个构造函数的参数。

不要使用与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缺少在argument labelsObjective-C中运行良好的Swift来识别一系列长的参数。所以在这里最好首先实例化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 ;
    }
}

答案 2 :(得分:0)

如果您不想创建日期,也可以在通话中构建日期:

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