在循环中创建对象并使用其他对象作为类构造函数

时间:2013-09-30 02:50:09

标签: java class object for-loop constructor

我对Java很新。我必须创建一个为员工建模的程序。该员工有一个员工编号,名字和姓氏,一个包含街道,城市和州的地址,以及一个月,一年和一天的雇用日期。这是我的代码:

import javax.swing.JOptionPane;

public class AssignmentTen
{
    public static void main (String[] args)
    {
        System.out.println();
        int num = Integer.parseInt(args[0]);
        int eNumber;
        String input2;
        String input3;
        String input4;
        String input5;
        String input6;
        int input7;
        int input8;
        int input9;
        int input10;

        Employee[] employee = new Employee[num]
        for (int i = 0; i < num, i++)
        employee[i] = new Employee()

        input2 = getString ("Enter Employee First Name:");
        input3 = getString ("Enter Employee Last Name:");
        input4 = getString ("Enter Employee Street:");
        input5 = getString ("Enter Employee City:");
        input6 = getString ("Enter Employee State (Initials):");
        input7 = getInt ("Enter Employee Zip Code (5 Digits):");
        input8 = getInt ("Enter Employee Hire Month (MM):");
        input9 = getInt ("Enter Employee Hire Day (DD):");
        input10 = getInt ("Enter Employee Hire Year(YYYY):");

        eNumber = getInt ("Enter Employee Number:");
        System.out.println("#" + eNumber);

        Name n1 = new Name(input2, input3);
        System.out.println(n1.firstName + " " + n1.lastName);

        Address a1 = new Address (input4, input5, input6, input7);
        System.out.println(a1.eStreet + " " + a1.eCity + " " + a1.eState + " " + a1.eZipCode);

        Date d1 = new Date (input8, input9, input10);
        System.out.println("Hire Date: " + d1.month + "/" + d1.day + "/" + d1.year);
    }

    public static int getInt(String paramString)
    {
        String str = JOptionPane.showInputDialog(paramString);
        return Integer.parseInt(str);
    }

    public static String getString(String paramString)
    {
        String str = JOptionPane.showInputDialog(paramString);
        return str;
    }
}

class Employee
{

}

class Name
{
    String firstName;
    String lastName;

    Name(String first, String last)
    {
        firstName = first;
        lastName = last;
    }
}

class Address
{
    String eStreet;
    String eCity;
    String eState;
    int eZipCode;

    Address(String street, String city, String state, int zipCode)
    {
        eStreet = street;
        eCity = city;
        eState = state;
        eZipCode = zipCode;
    }
}

class Date
{
    int month;
    int day;
    int year;

    Date(int eMonth, int eDay, int eYear)
    {
        month = eMonth;
        day = eDay;
        year = eYear;
    }
}

如何让Employee类使用Name,Address和Date类作为构造函数将所有员工的信息存储在一个对象中?另外,我需要在程序中有一个单独的Employee,Name,Address和Date类。另外,如何创建一个for循环,创建一个等于num整数(从命令行输入)的雇员对象,以便每个对象都有自己的Name,Date和Address?

我知道这段代码可能无法编译,我只需要完成它。

2 个答案:

答案 0 :(得分:0)

您需要为Employee类创建一个非默认构造函数,并允许您通过创建匹配实例将NameAddressDate参数与其关联变量

(在下面的代码中,您还必须为每个字段创建访问器方法)

class Employee
{
  private Name name;
  private Address address;
  private Date date;

  public Employee(Name name, Address address, Date date)
  {
    this.name = name;
    this.address = address;
    this.date = date;
  }
}

答案 1 :(得分:0)

员工拥有地址,名称和雇用日期,因此您必须声明类型为Address,Name和Date的实例变量。然后您可以编写一个构造函数,将Address,Name和Date作为构造函数参数并将它们分配给员工的实例变量


Class Employee{
    private Name name;
    private Address address;
    private Date hireDate;
    public Employee(Name name,Address address,Date hireDate){
           this.name = name;
           this.address = address;
           this.hireDate = hireDate;
        }
}

for loop

Employee[] employees = new Employee[noOfEmployees]

for(int i = 0; i < noOfEmployees; i++){
    Name name = new  Name(String first, String last);
    Address address = new Address(String street, String city, String state, int zipCode);
    Date hireDate = new  Date(int eMonth, int eDay, int eYear);
    employees[i] = new Employee(name,address,hireDate);
}

其中noOfEmployees是您要创建的员工数。 你可以使用java的内置Date类java.util.Date for hireDate。