在另一个 class 中对非静态方法进行静态引用
Making a static reference to a non-static method in another class
我正在学习 Java 课程中的面向对象编程,并且我正在做一个项目,其中程序创建三种类型的对象:地址、日期和员工。程序存储了几个员工的数据,然后将数据显示在Employee类型的数组中。
我正在使用四种不同的 classes:Address
class、Date
class 和 Employee
Class,以及创建数组的 EmployeeTest
class。
这是地址 class:
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);
}
}
日期 Class:
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);
}
}
而且,员工 Class:
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();
}
}
所有这些 class 都在同一个包中(我使用的是 Eclipse)。 Employee class 的要点是创建一个 Employee 类型的对象,并能够使用地址、姓名和日期 classes.
获取它的地址、姓名和 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[]
。代码的想法是,对于数组中的每个员工,获取其他 classes 中的所有变量:员工编号、员工姓名(名字和姓氏)、地址(街道地址、城市、州代码, 邮政编码),雇用日期(月、日、年)。获取所有这些后,第二个 for
循环遍历每个 Employee 并显示信息。
我遇到的问题是,在 Employee
class 中,Eclipse 在 getDate()
、getName()
和 getAddress()
中给我一个错误方法。例如,当我说 return Date.getDateString()
时,Eclipse 说我不能对非静态方法进行静态引用。它的解决方案是使 getDateString()
静态,我试过这个,但问题是通过使地址、员工和日期中的所有方法和变量成为 classes,值被锁定。这意味着将为所有员工显示相同的数据。
这就是我的意思。如果我将所有方法和变量设为静态,下面是一个示例输出。星号之间的文本是用户输入的内容。
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
通过将所有变量和方法设为静态,值如图所示被锁定,这使得程序无用。有人能解决这个问题吗?我需要一种方法来显示每个员工的信息,同时引用其他 classes 中的方法。现在,通常我会在一个名为 Employee
的 class 下创建所有变量和方法,但赋值说明指定我需要创建单独的 classes.
您正在为 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);
顺便说一句,你不应该大写变量,只能类。
变量应该是驼峰式的。
我正在学习 Java 课程中的面向对象编程,并且我正在做一个项目,其中程序创建三种类型的对象:地址、日期和员工。程序存储了几个员工的数据,然后将数据显示在Employee类型的数组中。
我正在使用四种不同的 classes:Address
class、Date
class 和 Employee
Class,以及创建数组的 EmployeeTest
class。
这是地址 class:
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);
}
}
日期 Class:
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);
}
}
而且,员工 Class:
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();
}
}
所有这些 class 都在同一个包中(我使用的是 Eclipse)。 Employee class 的要点是创建一个 Employee 类型的对象,并能够使用地址、姓名和日期 classes.
获取它的地址、姓名和 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[]
。代码的想法是,对于数组中的每个员工,获取其他 classes 中的所有变量:员工编号、员工姓名(名字和姓氏)、地址(街道地址、城市、州代码, 邮政编码),雇用日期(月、日、年)。获取所有这些后,第二个 for
循环遍历每个 Employee 并显示信息。
我遇到的问题是,在 Employee
class 中,Eclipse 在 getDate()
、getName()
和 getAddress()
中给我一个错误方法。例如,当我说 return Date.getDateString()
时,Eclipse 说我不能对非静态方法进行静态引用。它的解决方案是使 getDateString()
静态,我试过这个,但问题是通过使地址、员工和日期中的所有方法和变量成为 classes,值被锁定。这意味着将为所有员工显示相同的数据。
这就是我的意思。如果我将所有方法和变量设为静态,下面是一个示例输出。星号之间的文本是用户输入的内容。
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
通过将所有变量和方法设为静态,值如图所示被锁定,这使得程序无用。有人能解决这个问题吗?我需要一种方法来显示每个员工的信息,同时引用其他 classes 中的方法。现在,通常我会在一个名为 Employee
的 class 下创建所有变量和方法,但赋值说明指定我需要创建单独的 classes.
您正在为 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);
顺便说一句,你不应该大写变量,只能类。 变量应该是驼峰式的。