[英]Making a static reference to a non-static method in another class
我正在 Java 課程中學習面向對象編程,我正在做一個項目,其中程序創建三種類型的對象:地址、日期和員工。 該程序存儲多個員工的數據,然后將數據顯示在 Employee 類型的數組中。
我使用了四個不同的類: Address
類、 Date
類和Employee
類,以及創建數組的EmployeeTest
類。
這是地址類:
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[]
,然后創建一個大小為EmployeeAmount
的Employee[]
。 代碼的思路是,對於Array中的每個Employee,獲取其他類中的所有變量:Employee Number, Employee Name (first and last), Address (Street Address, City, State Code, Zip Code),雇用日期(月、日、年)。 獲得所有這些之后,第二個for
循環遍歷每個 Employee 並顯示信息。
我遇到的問題是,在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
類下創建所有變量和方法,但分配說明指定我需要創建單獨的類。
您正在為 for 循環的每次迭代創建Name
、 Address
和Date
。 但是您沒有辦法,也沒有在每個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);
順便說一句,您不應該將變量大寫,而應該將類大寫。 變量應該是駝峰式的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.