我正在Java课程中学习面向对象的编程,我正在做一个程序创建三种类型对象的项目:Address,Date和Employee。该程序存储多个员工的数据,然后以Employee类型的数组显示数据。
我使用了四个不同的类:Address
类,Date
类和Employee
类,以及创建数组的EmployeeTest
类。< / p>
这是Address类:
public class Address {
private String Street;
private String City;
private String State;
private int ZipCode;
public Address(String St, String Ci, String Sta, int Zip){
Street = St;
City = Ci;
State = Sta;
ZipCode = Zip;
}
public String getEmployeeAddress(){
return (Street + ", " + City + ", " + State + " " + ZipCode);
}
}
日期类:
public class Date {
private int Month;
private int Day;
private int Year;
public Date(int M, int D, int Y){
Month = M;
Day = D;
Year = Y;
}
public String getDateString(){
return (Month + "/" + Day + "/" + Year);
}
}
而且,员工类:
public class Employee {
private int EmployeeNum;
public void setEmployeeNum(int ENum){
EmployeeNum = ENum;
}
public int getNum(){
return EmployeeNum;
}
public String getDate(){
return Date.getDateString();
}
public String getName(){
return Name.getEmployeeName();
}
public String getAddress(){
return Address.getEmployeeAddress();
}
}
所有这些类都在同一个包中(我正在使用Eclipse)。 Employee类的要点是创建一个Employee类型的对象,并且能够使用Address,Name和Date类来获取它的Address,Name和HireDate。
阵列发挥作用的地方在这里:
import java.util.Scanner;
import java.lang.*;
public class EmployeeTest {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("How many employees will have their data stored today?");
int EmployeeAmount = Integer.parseInt(input.nextLine());
Employee [] EmployeeArray = new Employee[EmployeeAmount];
for (int i = 0; i < EmployeeArray.length; i ++){
System.out.print("What is employee " + (i+1) + "'s employee number?");
int EmployeeNumber = Integer.parseInt(input.nextLine());
EmployeeArray[i] = new Employee();
EmployeeArray[i].setEmployeeNum(EmployeeNumber);
System.out.println("What is the first name of employee " + EmployeeNumber + "?");
String EmployeeFirstName = input.nextLine();
System.out.println("What is the last name of employee " + EmployeeNumber + "?");
String EmployeeLastName = input.nextLine();
Name EmployeeName = new Name(EmployeeFirstName, EmployeeLastName);
System.out.println("Please enter the street address: ");
String StreetAddress = input.nextLine();
System.out.println("Please enter the name of the city: ");
String CityName = input.nextLine();
System.out.println("Please enter the two character code for the state: ");
String StateID = input.nextLine();
System.out.println("Please enter this address's zip code: ");
int ZipCode = Integer.parseInt(input.nextLine());
Address EmployeeAddress = new Address(StreetAddress, CityName, StateID, ZipCode);
System.out.println("Finally, what was the month(#) of the hire date?");
int Month = Integer.parseInt(input.nextLine());
System.out.println("What was the day(#)?");
int Day = Integer.parseInt(input.nextLine());
System.out.println("What was the year?");
int Year = Integer.parseInt(input.nextLine());
Date HireDate = new Date(Month, Day, Year);
}
for (int j = 0; j < EmployeeArray.length; j ++){
System.out.println("Employee number: " + EmployeeArray[j].getNum());
System.out.println("Employee Name: " + EmployeeArray[j].getName());
System.out.println("Employee Address: " + EmployeeArray[j].getAddress());
System.out.println("Employee Hiredate: " + EmployeeArray[j].getDate());
}
}
}
程序会提示用户输入要存储在数组中的员工数,然后创建大小为Employee[]
的{{1}}。代码的想法是,对于Array中的每个Employee,获得其他类中的所有变量:Employee Number,Employee Name(first and last),Address(Street Address,City,State Code,Zip Code),雇用日期(月,日,年)。获得所有这些后,第二个EmployeeAmount
循环遍历每个Employee并显示信息。
我遇到的问题是,在for
类中,Eclipse在Employee
,getDate()
和getName()
方法中给出了错误。当我说getAddress()
时,Eclipse说我无法对非静态方法进行静态引用。它的解决方案是使return Date.getDateString()
静态,我尝试了这个,但问题是通过在Address,Employee和Date类中创建所有方法和变量,值被锁定。这意味着将为所有员工显示相同的数据。
这就是我的意思。下面是一个示例输出,如果我将所有方法和变量设置为静态。星号之间的文本是用户输入的内容。
getDateString()
通过使所有变量和方法保持静态,值如图所示被锁定,这使得程序无用。有没有人有这个问题的解决方案?我需要一种方法来显示每个员工的信息,同时引用其他类中的方法。现在,通常我会在一个名为How many employees will have their data stored today?**2**
What is employee 1's employee number?**1**
What is the first name of employee 1?
**Bob**
What is the last name of employee 1?
**Jones**
Please enter the street address:
**300 1st Avenue**
Please enter the name of the city:
**New York**
Please enter the two character code for the state:
**NY**
Please enter this address's zip code:
**10001**
Finally, what was the month(#) of the hire date?
**1**
What was the day(#)?
**1**
What was the year?
**2001**
What is employee 2's employee number?**2**
What is the first name of employee 2?
**Bobby**
What is the last name of employee 2?
**Robinson**
Please enter the street address:
**301 1st Avenue**
Please enter the name of the city:
**Los Angeles**
Please enter the two character code for the state:
**CA**
Please enter this address's zip code:
**90001**
Finally, what was the month(#) of the hire date?
**1**
What was the day(#)?
**2**
What was the year?
**2004**
Employee number: 2
Employee Name: Bobby Robinson
Employee Address: 301 1st Avenue, Los Angeles, CA 90001
Employee Hiredate: 1/2/2004
Employee number: 2
Employee Name: Bobby Robinson
Employee Address: 301 1st Avenue, Los Angeles, CA 90001
Employee Hiredate: 1/2/2004
的类下创建所有变量和方法,但是赋值指令指定我需要创建单独的类。
答案 0 :(得分:1)
您正在为for循环的每次迭代创建Name
,Address
和Date
。但是你没有办法,也没有在每个Employee
实例上设置它们。
您需要添加方法来设置每个Employee上的值并存储信息。像这样:
public class Employee {
private int employeeNum;
private Name name;
private Date hireDate;
private Address address;
public void setEmployeeNum(int eNum){
employeeNum = eNum;
}
public int getEmployeeNum(){
return employeeNum;
}
public void setHireDate(Date date){
this.hireDate = date;
}
public String getHireDate(){
return hireDate.getDateString();
}
public void setName(Name n){
this.name = n;
}
public String getName(){
return name.getEmployeeName();
}
public void setAddress(Address addy){
this.address = addy;
}
public String getAddress(){
return address.getEmployeeAddress();
}
}
然后在你的for循环中,设置值:
EmployeeArray[i].setName(EmployeeName);
EmployeeArray[i].setAddress(EmployeeAddress);
EmployeeArray[i].setHireDate(HireDate);
顺便说一下,你不应该把变量,只有类大写。 变量应该是驼峰式的。