简体   繁体   English

对另一个类中的非静态方法进行静态引用

[英]Making a static reference to a non-static method in another class

I'm learning object-oriented programming in a Java course, and I'm doing a project where a program creates three types of objects: Address, Date, and Employee.我正在 Java 课程中学习面向对象编程,我正在做一个项目,其中程序创建三种类型的对象:地址、日期和员工。 The program stores data of several employees and then displays the data in an array of type Employee.该程序存储多个员工的数据,然后将数据显示在 Employee 类型的数组中。

I'm using four different classes: an Address class, a Date class, and an Employee Class, and an EmployeeTest class that creates the array.我使用了四个不同的类: Address类、 Date类和Employee类,以及创建数组的EmployeeTest类。

Here is the Address 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);
    }
}

The Date 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);
    }
}

And, the Employee 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();
    }

}

All of these classes are in the same package (I'm using Eclipse).所有这些类都在同一个包中(我使用的是 Eclipse)。 The point of the Employee class is to create an object of type Employee and be able to get it's Address, Name, and HireDate using the Address, Name, and Date classes. Employee 类的重点是创建一个 Employee 类型的对象,并能够使用 Address、Name 和 Date 类获取它的 Address、Name 和 HireDate。

The place where the array comes into play is here:数组发挥作用的地方在这里:

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());
        }


    }

}

The program prompts the user for the number of employees to be stored in the array, then creates an Employee[] of size EmployeeAmount .该程序提示用户输入要存储在数组中的Employee[] ,然后创建一个大小为EmployeeAmountEmployee[] The idea of the code is that for each Employee in the Array, all of the variables in the other classes are obtained: Employee Number, Employee Name (first and last), Address (Street Address, City, State Code, Zip Code), Hire Date (Month, Day, Year).代码的思路是,对于Array中的每个Employee,获取其他类中的所有变量:Employee Number, Employee Name (first and last), Address (Street Address, City, State Code, Zip Code),雇用日期(月、日、年)。 After all this is obtained, the second for loop iterates through each Employee and displays the info.获得所有这些之后,第二个for循环遍历每个 Employee 并显示信息。

The problem I am having is that in the Employee class, Eclipse gives me an error in the getDate() , getName() , and getAddress() methods.我遇到的问题是,在Employee类中,Eclipse 在getDate()getName()getAddress()方法中给了我一个错误。 When I say for example, return Date.getDateString() , Eclipse says that I cannot make a static reference to a non-static method.例如,当我说return Date.getDateString() ,Eclipse 说我不能对非静态方法进行静态引用。 It's solution is to make getDateString() static, and I tried this, but the problem is that by making all the methods and variables in the Address, Employee, and Date classes, the values are locked.解决方案是将getDateString()静态,我尝试了此方法,但问题是通过使 Address、Employee 和 Date 类中的所有方法和变量,值被锁定。 Meaning that the same data will be displayed for all employees.这意味着将为所有员工显示相同的数据。

Here's what I mean.这就是我的意思。 Below is a sample output if I made all the methods and variables static.如果我将所有方法和变量设为静态,下面是一个示例输出。 The text in between the asterisks is what the user inputs.星号之间的文本是用户输入的内容。

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

By making all of the variables and methods static, the values are locked as shown, which makes the program useless.通过将所有变量和方法设为静态,值被锁定,如图所示,这使得程序无用。 Does anyone have a solution to this problem?有没有人有解决这个问题的方法? I need a way to display the information of each employee while referencing the methods in the other classes.我需要一种方法来显示每个员工的信息,同时引用其他类中的方法。 Now, normally I would just create all the variables and methods under one class called Employee , but the assignment instructions specify that I need to make individual classes.现在,通常我只会在一个名为Employee类下创建所有变量和方法,但分配说明指定我需要创建单独的类。

You are creating a Name , Address , and Date for each iteration of the for loop.您正在为 for 循环的每次迭代创建NameAddressDate But you have not way, nor do you set them on each Employee instance.但是您没有办法,也没有在每个Employee实例上设置它们。

You need to add methods to set the values on each Employee and store the information.您需要添加方法来设置每个员工的值并存储信息。 Something like this:像这样的东西:

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();
    }
}

Then in your for loop, set the values:然后在您的 for 循环中,设置值:

  EmployeeArray[i].setName(EmployeeName);
  EmployeeArray[i].setAddress(EmployeeAddress);
  EmployeeArray[i].setHireDate(HireDate);

By the way, you should not capitalize variables, only classes.顺便说一句,您不应该将变量大写,而应该将类大写。 Variables should be camel-cased.变量应该是驼峰式的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM