类构造函数对象无法正确显示

时间:2013-10-01 02:29:29

标签: java class object command-line constructor

如果您想知道我的计划的目的是什么,您可以查看我以前的问题。这是我几乎完成的代码:

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[] employees = new Employee[num];
        for (int i = 0; i < num; i++)
        {
            eNumber = getInt ("Enter Employee Number:");
            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):");

            Name name = new Name(input2, input3);
            Address address = new Address (input4, input5, input6, input7);
            Date hireDate = new Date (input8, input9, input10);
            employees[i] = new Employee (eNumber, name, address, hireDate);

            System.out.println("#" + employees[i].empNumber + "\n" + employees[i].empName + "\n" + employees[i].empAddress + "\nHire Date: " + employees[i].empHireDate + "\n\n");
        }
    }

    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
{
    Number empNumber;
    Name empName;
    Address empAddress;
    Date empHireDate;

    public Employee(Number empNumber, Name empName, Address empAddress, Date empHireDate)
    {
        this.empNumber = empNumber;
        this.empName = empName;
        this.empAddress = empAddress;
        this.empHireDate = empHireDate;
    }
}

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;
    }
}

我希望我的程序显示通过命令行指定的员工数量的信息(例如,我输入'java AssignmentTen 3'来记录三个不同员工对象的数据)。我希望信息显示如下:

  

[#] EmployeeNumber

     

FirstName LastName

     

Street City StateInitials ZipCode

     

雇用日期:MM DD YYYY

例如:

  

#123

     

John Smith

     

123示例街道CA 12345

     

聘用日期:2013年9月30日

然而,最终显示的内容根本不正确。员工编号显示正常,但名称行显示'Name @ [RandomCharacters]',地址行显示'Address @ [RandomCharacters]',而雇用日期行显示'雇用日期:日期@ [RandomCharacters]'。如何才能显示我想要的方式(包括单词和斜杠之间的空格除以月,日和年)?

感谢。

编辑:我添加了接受的答案建议的更改,但我仍然遇到日期行的问题。我用了这段代码:

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

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

    public String toString()
    {
        return month + "/" + day + "/" + year;
    }
}

但它仍然是随机字符。

3 个答案:

答案 0 :(得分:1)

您看到的字符是您的类从toString()类继承的非重写Object方法的结果(因为所有类都从Object扩展)。您需要实现自定义toString()方法,以返回每个类所需的格式。例如,

class Name
{
    String firstName;
    String lastName;

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

    public String toString() 
    {
        return "[firstName = " + firstName + ", lastName = " + lastName + "]";
    }
}

然后你可以做

Name name = new Name("John", "Doe");
System.out.println(name);

会打印

[firstName = John, lastName = Doe]

答案 1 :(得分:0)

您需要为toStringNameAddress中的每一个编写Date方法。这是在您尝试打印任何对象时在幕后调用的内容。以下是toString类的Date方法的外观。请自己动手做另外两个。

public String toString() {
    return month + "/" + day + "/" + year;
}

您可能会发现阅读http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()很有用。

答案 2 :(得分:0)

您添加到员工的姓名,地址和日期变量是具有自己变量的OBJECTS。要么覆盖toString()以打印出每个变量,要么通过employees [i] .name.firstname手动调用它们。

@Override
public String toString(){
    return firstName + lastName;
}

此外,您应该将私有变量与getter和setter访问方法一起使用。