使用多个类时的奇数输出

时间:2013-09-07 19:58:28

标签: java arrays object output

我是java编程的新手,我即将完成一个非常大的项目。我正在尝试建立一个简单地传回信息的员工注册表。每当我输入信息时,它只会返回名称@ 5a965654之类的内容。我的课程如下,任何帮助都将不胜感激。

主:

import java.util.Scanner;

public class Main 
{
    public static void main(String[] args)
    {
        Scanner Input = new Scanner(System.in);

        System.out.println("Enter the number of employees to enter.");
        int employeeCount = Input.nextInt();
        Input.nextLine();

        Employee employees[] = new Employee[employeeCount];

        String firstName;
        String lastName;
        String street;
        String city;
        String state;
        String zipCode;
        String monthHired;
        String dateHired;
        String yearHired;
        int employeeID;

        for(int x = 0; x < employeeCount; x++)
        {
            System.out.println("Please enter the first name of employee " + (x + 1));
            firstName = Input.nextLine();
            System.out.println("Please enter the last name of employee " + (x + 1));
            lastName = Input.nextLine();
            System.out.println("Please enter the street of employee " + (x + 1));
            street = Input.nextLine();
            System.out.println("Please enter the city of employee " + (x + 1));
            city = Input.nextLine();
            System.out.println("Please enter the state of employee " + (x + 1));
            state = Input.nextLine();
            System.out.println("Please enter the zip code of employee " + (x + 1));
            zipCode = Input.nextLine();
            System.out.println("Please enter the month hired for employee " + (x + 1));
            monthHired = Input.nextLine();
            System.out.println("Please enter the date hired for employee " + (x + 1));
            dateHired = Input.nextLine();
            System.out.println("Please enter the year hired for employee " + (x + 1));
            yearHired = Input.nextLine();
            Name name = new Name(firstName, lastName);
            name.setName(firstName, lastName);
            Address address = new Address(street, city, state, zipCode);
            DateOfHire hireDate = new DateOfHire(monthHired, dateHired, yearHired);
            employees[x] = new Employee(name, address, hireDate, x);
        }

        for(int x = 0; x < employeeCount; x++)
        {
            employees[x].printInfo(x);
        }
    }
}

员工类:

public class Employee 
{
    private Name name;
    private Address address;
    private DateOfHire hireDate;
    int ID;

    public Employee()
    {

    }

    public Employee(Name name, Address address, DateOfHire hireDate, int x)
    {
        this.name = name;
        this.address = address;
        this.hireDate = hireDate;
        this.ID = x;
    }

    public void printInfo(int x)
    {
        System.out.println("Employee-" + (x + 1));
        System.out.println("Name: " + this.name);
        System.out.println("Address: " + this.address);
        System.out.println("Date of Hire: " + this.hireDate);
    }
}

Name,DateHired和Address类的格式:

public class Name 
{
    private String firstName;
    private String lastName;

    public Name()
    {

    }

    public Name(String firstName, String lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public void setName(String firstName, String lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getName()
    {
        return firstName + " " + lastName;
    }
}

3 个答案:

答案 0 :(得分:2)

NameString不同,因此当您在this.name中打印Employee.printInfo时,会打印Name@[numbers],表明您的'{1}}重新打印是数字所描述位置的Name对象。

尝试用

替换该行
System.out.println("Name: " + this.name.getName());

另外,你需要为AddressDateOfHire执行类似的操作,但我不知道你为这些实现了什么,所以我不能真正说出具体的内容做。但基本上,你需要一个方法来给出你想要打印的任何对象的字符串表示。

答案 1 :(得分:1)

Java中的所有类都来自java.lang.Object,它具有方法toString()。该方法实现为

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

每当你打电话

System.out.println("Name: " + this.name);

字符串连接是通过隐式调用实例的toString()方法完成的。如果您的类未实现(覆盖)toString()方法,则会使用Object的实现。

See the String Conversion rules in the Java Language Specification.

  

否则,转换就像通过调用一样执行   没有参数的引用对象的toString方法;但如果   调用toString方法的结果为null,则字符串为“null”   而是用来代替。

由于您的Name类没有toString()方法,因此调用其父类'方法,即。 Object#toString(),你得到你看到的输出。

您应该覆盖所有课程中的toString()方法。例如,

public class Name 
{
    private String firstName;
    private String lastName;

    public Name()
    {

    }

    public Name(String firstName, String lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public void setName(String firstName, String lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }

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

    public String getName()
    {
        return firstName + " " + lastName;
    }
}

toString()无关,getName()在这种情况下返回相同的内容。你必须遵循语言规范。

答案 2 :(得分:0)

如果要打印对象,应该实现String toString()方法