gpt4 book ai didi

java - 使用多个类时输出奇怪

转载 作者:行者123 更新时间:2023-11-29 05:38:15 25 4
gpt4 key购买 nike

我是 Java 编程的新手,我即将完成一个非常大的项目。我正在尝试制作一个简单地回传信息的员工注册表。每当我输入信息时,它只会返回诸如 Name@5a965654 之类的内容。我的类(class)如下,如有任何帮助,我们将不胜感激。

主要内容:

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

最佳答案

A NameString 不同, 所以当你打印 this.nameEmployee.printInfo , 它打印 Name@[numbers] , 表示您正在打印的是 Name对象位于数字所描述的位置。

尝试用

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

此外,您还需要为 Address 执行类似的操作和 DateOfHire ,但我不知道你为那些实现了什么,所以我真的不能说具体要做什么。不过,从本质上讲,您需要一个方法来提供您要打印的任何对象的字符串表示形式。

关于java - 使用多个类时输出奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18677343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com