gpt4 book ai didi

java - java中访问对象变量

转载 作者:行者123 更新时间:2023-12-02 06:20:17 25 4
gpt4 key购买 nike

所以我正在做一个java作业,内容如下:

“编写一个对员工进行建模的程序。员工有员工编号、姓名、地址和雇用日期。姓名由名字和姓氏组成。地址由街道、城市、州(2 个字符)和 5 位邮政编码。日期由整数月、日和年组成。在解决方案中使用 Employee 类、Name 类、Address 类和 Date 类。提供适当的类构造函数、getter 方法、setter 方法以及您认为必要的任何其他方法。您的程序应提示用户输入多个员工的数据,然后显示该数据。应从命令行输入要存储数据的员工数量。”

由于我在格式化时遇到问题,因此无法发布代码,但我所做的是创建一个员工对象数组。该数组的标题为 dbase[]。然后,每个 Employee 对象为日期、姓名和地址标题 date1、name1 和 address1 创建一个单独的对象。然而,每当我尝试使用类似 dbase[j].name1.getFirstName 的方法访问日期/名称/地址中的 getter 方法之一时,我都会遇到一个错误,提示 cannot find symbol: name 1 ---位置:Employee类

public class EmployeeProgram {
public static void main (String[] args) {
int i = Input.getInt ("How many employees would you like to enter? ");
int j;
Employee [] dbase = new Employee [i];
for (j = 0; j < i; j++) {
String firstName = Input.getString ("What is an employee's first name?");
String lastName = Input.getString ("What is this employee's last name?");

String street = Input.getString ("On what street does this employee live?");
String city = Input.getString ("In which city does this employee live?");
String state = Input.getString ("In which state does this employee live? (abbreviation)");
String zipcode = Input.getString ("What is this employee's zip code?");

int month = Input.getInt ("In what month was this employee born?");
int day = Input.getInt ("On what day was this employee born?");
int year = Input.getInt ("In what year was this employee born?");

int employeeID = Input.getInt ("What should this employee's employee id be?");

dbase[j] = new Employee(firstName, lastName, street, city, state, zipcode, month, day, year, employeeID);
}

for (j = 0; j < i; j++) {
System.out.print ( "Employee number " + (j + 1) + " is named ");
System.out.print ( dbase[j].name.getFirst() + " " + dbase[j].name.getLast() + " and lives on " + dbase[j].name.getStreet());
System.out.print ( " in " + dbase[j].address.getCity() + " " + dbase[j].address.getState() + ", " + dbase[j].address.getZip());
System.out.print ( ". He will be hired on " + dbase[j].date.GetMonth() + "-" + dbase[j].date.getDay() + "-" + dbase[j].date.getYear() );
System.out.print ( " and his ID is " + dbase[j].getEmployeeID());
System.out.println ();
}
}
}

class Employee {
int employeeID = 0;
Employee( String firstName1, String lastName1, String street1, String city1, String state1, String zipcode1, int month1, int day1, int year1, int employeeID1 ) {
name name = new name( firstName1, lastName1 );
address address = new address( street1, city1, state1, zipcode1 );
date date = new date( month1, day1, year1);
employeeID = employeeID1;
}
int getEmployeeID() {
return employeeID;
}
}

class name {
String firstName = "";
String lastName = "";
name(String newFirstName, String newLastName) {
firstName = newFirstName;
lastName = newLastName;
}
String getFirst() {
return firstName;
}
String getLast() {
return lastName;
}
}

我没有发布地址或日期类,但它们与名称类基本相同

最佳答案

您尚未在 Employee 类中创建任何字段。只是构造函数内的局部变量。所以添加

private Name name;
private Address address;

...(类名始终大写)并为它们添加 getter 和 setter。

在你的 main 中你应该使用

dbase[j].getName().getFirst()

等等...

关于java - java中访问对象变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21081865/

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