简体   繁体   English

类构造器对象无法正确显示

[英]Class Constructor Objects don't display correctly

You can check my previous questions if you want to know what the purpose of my program is. 如果您想知道我的程序的目的,可以查看我以前的问题。 Here is my almost finished code: 这是我几乎完成的代码:

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

I want my program to display information for an amount of employees specified through the command line (so for example, I type 'java AssignmentTen 3' to record data for three different employee objects). 我希望我的程序显示有关通过命令行指定的雇员数量的信息(例如,我键入“ java AssignmentTen 3”来记录三个不同雇员对象的数据)。 I would like the information to display like this: 我希望信息显示如下:

[#]EmployeeNumber [#]员工编号

FirstName LastName 名姓

Street City StateInitials ZipCode 街市州首府邮政编码

Hire Date: MM DD YYYY 录用日期:MM DD YYYY

So for example: 因此,例如:

#123 #123

John Smith 约翰·史密斯

123 Example Street CA 12345 123示例街CA 12345

Hire Date: 09/30/2013 雇用日期:09/30/2013

However, what ends up displaying isn't correct at all. 但是,最终显示的内容根本不正确。 The Employee Number displays fine, but then the Name line reads 'Name@[RandomCharacters]', the Address line reads 'Address@[RandomCharacters]', and the Hire Date line reads 'Hire Date: Date@[RandomCharacters]'. 员工编号显示正常,但是“名称”行显示为“ Name @ [RandomCharacters]”,“地址”行显示为“ Address @ [RandomCharacters]”,“雇用日期”行显示为“雇用日期:Date @ [RandomCharacters]”。 How can I make it so it displays the way I want (including the spaces between words and the slashes dividing the month, day, and year)? 如何使它显示我想要的方式(包括单词之间的空格和将月,日和年分隔的斜杠)?

Thanks. 谢谢。

Edit: I added the changes the accepted answer suggested, but I'm still running into trouble with the Date line. 编辑:我添加了接受的答案建议的更改,但我仍然在日期行遇到麻烦。 I used this code: 我使用以下代码:

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

But it still came up with random characters. 但是它仍然想出了随机字符。

The characters you are seeing are the result of the un-overriden toString() method your class inherits from Object class (since all classes extend from Object ). 您看到的字符是您的类从Object类继承的toString()方法的结果(因为所有类都从Object扩展)。 You need to implement your custom toString() method to return the format you want for each of your classes. 您需要实现自定义的toString()方法以返回每个类所需的格式。 For example, 例如,

class Name
{
    String firstName;
    String lastName;

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

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

Then you can do 那你可以做

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

would print 会打印

[firstName = John, lastName = Doe]

You need to write a toString method for each of Name , Address and Date . 您需要为NameAddressDate分别编写一个toString方法。 This is what gets called behind the scenes when you try to print any object. 当您尝试打印任何对象时,这就是所谓的幕后。 Here's what the toString method might look like for the Date class. 这是Date类的toString方法的外观。 Have a go at doing the other two for yourself. 自己去做另外两个。

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

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

Your Name, Address, and Date variables that you add to employee are OBJECTS with their own variables. 您添加到员工的姓名,地址和日期变量是带有其自身变量的对象。 Either override toString() to print out each variable or manually call them via employees[i].name.firstname. 覆盖toString()以打印出每个变量,或者通过employee [i] .name.firstname手动调用它们。

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

Also you should be using private variables with getter and setter access methods. 另外,您应该将私有变量与getter和setter访问方法一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM