在java中访问对象变量
因此,我正在进行一项java任务,内容如下: “编写一个模拟员工的程序。员工有员工编号、姓名、地址和雇用日期。姓名由姓和名组成。地址由街道、城市、州(2个字符)和5位邮政编码组成。日期由整数月、日和年组成。”。 在解决方案中使用Employee类、Name类、Address类和Date类。提供适当的类构造函数、getter方法、setter方法以及您认为必要的任何其他方法。 您的程序应提示用户输入多个员工的数据,然后显示该数据。存储数据的员工人数应通过命令行输入。” 由于格式化有问题,我无法发布代码,但我所做的是创建一个employee对象数组。该数组的标题为dbase[]。然后,每个Employee对象为date、name和address title date1、name1和address1创建一个单独的对象。但是,每当我尝试使用类似于dbase[j].name1.getFirstName的东西访问date/name/address中的一个getter方法时,我都会遇到一个错误,错误是找不到symbol:name 1---location:class Employee在java中访问对象变量,java,Java,因此,我正在进行一项java任务,内容如下: “编写一个模拟员工的程序。员工有员工编号、姓名、地址和雇用日期。姓名由姓和名组成。地址由街道、城市、州(2个字符)和5位邮政编码组成。日期由整数月、日和年组成。”。 在解决方案中使用Employee类、Name类、Address类和Date类。提供适当的类构造函数、getter方法、setter方法以及您认为必要的任何其他方法。 您的程序应提示用户输入多个员工的数据,然后显示该数据。存储数据的员工人数应通过命令行输入。” 由于格式化有问题,我无法发布
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;
}
}
公共类雇员程序{
公共静态void main(字符串[]args){
int i=Input.getInt(“您希望输入多少员工?”);
int j;
员工[]数据库=新员工[i];
对于(j=0;j
我没有发布address或date类,但它们基本上与name类相同您在Employee类中没有创建任何字段。只是构造函数中的局部变量。所以加上
private Name name;
private Address address;
。。。
(类名总是大写)
并为它们添加getter和setter
在你的主要任务中,你应该使用
dbase[j].getName().getFirst()
等等。据我所知,您的主要问题是您的Employee成员不是字段,而是构造函数中的局部变量。这意味着当构造函数结束时,它们基本上消失了。您应该能够移动它们,使它们成为字段,程序将编译 几个小音符:
- 在Java中,所有类都以大写字母开头。(
应为类名
等)类名
- 像Employee这样的类应该将其字段作为私有字段,并通过“get”方法进行访问。实际上有一个类似类的标准,叫做。这对于初学者来说并不重要,因为封装是您将学习的内容。本教程显示了一个具有“private with accessors”方案的类
class Employee {
name name;
address address;
date date;
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 = new name( firstName1, lastName1 );
address = new address( street1, city1, state1, zipcode1 );
date = new date( month1, day1, year1);
employeeID = employeeID1;
}
int getEmployeeID() {
return employeeID;
}
}