gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:03:46 25 4
gpt4 key购买 nike

如果你想知道我的程序的目的是什么,你可以查看我之前的问题。这是我即将完成的代码:

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

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

[#]EmployeeNumber

FirstName LastName

Street City StateInitials ZipCode

Hire Date: MM DD YYYY

例如:

#123

John Smith

123 Example Street CA 12345

Hire Date: 09/30/2013

但是,最终显示的内容根本不正确。员工编号显示正常,但姓名行显示“姓名@[随机字符]”,地址行显示“地址@[随机字符]”,雇用日期行显示“雇用日期:日期@[随机字符]”。我怎样才能让它以我想要的方式显示(包括单词之间的空格以及分隔月、日和年的斜杠)?

谢谢。

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

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

但它仍然出现随机字符。

最佳答案

您看到的字符是您的类从 Object 类继承的未重写的 toString() 方法的结果(因为所有类都从 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]

关于java - 类构造函数对象无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19107001/

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