1

我对 java 编程很陌生,我即将完成一个非常大的项目。我正在尝试制作一个简单地将信息传递回来的员工注册表。每当我输入信息时,它只会返回诸如 Name@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;
    }
}
4

3 回答 3

2

AName和 a 不一样String,所以当你打印this.namein时Employee.printInfo,它会打印Name@[numbers],表示你打印的是一个Name对象,位于数字所描述的位置。

尝试将该行替换为

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

另外,你需要对Addressand做类似的事情DateOfHire,但我不知道你为这些实现了什么,所以我真的不能说具体要做什么。不过,从本质上讲,您需要一种方法,该方法可以提供要打印的任何对象的字符串表示形式。

于 2013-09-07T20:02:26.263 回答
1

Java 中的所有类都继承自java.lang.Objectwhich 有一个方法toString()。该方法实现为

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

每当你打电话

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

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

请参阅 Java 语言规范中的字符串转换规则。

否则,转换就像调用被引用对象的 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()返回相同的东西无关紧要。您必须遵循语言规范。

于 2013-09-07T20:04:17.610 回答
0

如果你想打印一个对象,你应该实现 String toString() 方法

于 2013-09-07T20:01:47.067 回答