【问题标题】:How to create an object of one class with arguments of another? Java如何使用另一个类的参数创建一个类的对象?爪哇
【发布时间】:2016-11-14 01:08:39
【问题描述】:

我正在创建一个名为“Employee”的用户定义类,它将保存有关员工的基本数据。例如姓名、生日和入职日期。

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

Employee 类的生日和入职日期必须是 Date 类型。

这是我的 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”)我得到一个错误

"必填:字符串、字符串、日期、日期

找到:字符串、字符串、int、int、int、int、int、int"。

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

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

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

谢谢

【问题讨论】:

  • 请更正您的代码 sn-p/ 您正在使用 date1 和 Date2 声明 Date 对象,它们是未声明的类。请改用日期。

标签: java date


【解决方案1】:

您需要传递一个 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));

【讨论】:

  • 您可以添加另一个构造函数,该构造函数直接采用m, d, y 参数并调用现有的构造函数以回答标题中的问题。
【解决方案2】:

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 缺少在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 ;
    }
}

【讨论】:

  • 6 个连续的 int 参数可能太多,用户无法记住。坚持 ISO-8601 肯定会有所帮助。
  • @LukeLee 我从来没有说过年月日的争论是个好主意!我说我遵循的建议是我们更直接地回答所提出的问题,这涉及创建“一个类的对象和另一个类的参数”。
【解决方案3】:

如果您不想在通话中创建日期之前创建日期:

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

【讨论】:

    猜你喜欢
    • 2015-07-17
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 2011-12-29
    • 2012-11-19
    • 2011-08-19
    • 1970-01-01
    • 2018-06-12
    相关资源
    最近更新 更多