我在Java課程中學習面向對象的編程,我正在做一個項目,其中程序創建三種類型的對象:Address,Date和僱員。該程序存儲多個員工的數據,然後將數據顯示在Employee類型的數組中。對另一個類中的非靜態方法進行靜態引用
我使用四種不同的類:一個Address
類,一類Date
和Employee
類,並且創建該陣列的EmployeeTest
類。
下面是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);
}
}
Date類:
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);
}
}
而且,Employee類:
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());
}
}
}
程序提示存儲陣列中的用戶員工的數量,然後創建大小EmployeeAmount
的Employee[]
。代碼的想法是,對於Array中的每個Employee,都會獲得其他類中的所有變量:Employee Number,Employee Name(first and last),Address(街道地址,城市,州代碼,郵政編碼),聘用日期(月,日,年)。在獲得所有這些信息後,第二個for
循環遍歷每個員工並顯示信息。
我遇到的問題是在Employee
類中,Eclipse給我提供了getDate()
,getName()
和getAddress()
方法的錯誤。例如,當我說return Date.getDateString()
時,Eclipse說我不能對非靜態方法進行靜態引用。它的解決辦法是使getDateString()
成爲靜態的,我試過這個,但問題是通過在Address,Employee和Date類中創建所有的方法和變量,這些值被鎖定。這意味着將爲所有員工顯示相同的數據。
這是我的意思。下面是一個示例輸出,如果我使所有的方法和變量都是靜態的。星號之間的文字是用戶輸入的內容。
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
通過使所有的變量和靜態方法的,這些值被鎖定如圖所示,這使得程序無用。有沒有人有解決這個問題?我需要一種方式來顯示每個員工的信息,同時引用其他類中的方法。現在,通常我會在一個名爲Employee
的類中創建所有變量和方法,但賦值說明指定我需要創建單個類。
員工應該有一個「日期」和「地址」(並且可能是「姓名」)的實例 – MadProgrammer
那麼這些實例應該與用戶輸入相同嗎? – LightFlicker
不要讓所有的方法都是靜態的或者你的變量。刪除靜態,創建類的實例並調用實例上的方法。 – pczeus