【问题标题】:Making a static reference to a non-static method in another class对另一个类中的非静态方法进行静态引用
【发布时间】:2016-04-01 02:18:17
【问题描述】:

我正在学习 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());
        }


    }

}

程序提示用户输入要存储在数组中的员工人数,然后创建一个大小为EmployeeAmountEmployee[]。代码的思路是,对于 Array 中的每个 Employee,获取其他类中的所有变量:Employee Number,Employee Name(first and last),Address(街道地址,City,State Code,Zip Code),雇用日期(月、日、年)。获得所有这些后,第二个for 循环遍历每个 Employee 并显示信息。

我遇到的问题是在Employeeclass 中,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 的类下创建所有变量和方法,但赋值指令指定我需要创建单独的类。

【问题讨论】:

  • 员工应该有一个属于自己的DateAddress(可能还有Name)的实例
  • 那么这些实例应该和用户输入的一样吗?
  • 不要将所有方法或变量设为静态。删除静态,创建类的实例并调用实例上的方法。
  • 第 1 步:遵循 Java 命名约定,并且始终以 小写字母 开头变量名类名大写字母开头。对我来说,阅读您的代码是不可能的,而且 StackOverflow 上的语法着色也取决于您是否遵循标准的 Java 命名约定。也和你对静态方法和实例方法的混淆有关
  • 对,但是一旦我在 EmployeeTest 类中创建了实例,我应该将这些实例等同于用户输入值吗?

标签: java arrays class oop class-design


【解决方案1】:

您正在为 for 循环的每次迭代创建 NameAddressDate。但是您没有办法,也没有在每个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);

顺便说一句,你不应该大写变量,只有类。 变量应该是驼峰式的。

【讨论】:

  • 你应该支持可变性的不变性;)
猜你喜欢
  • 2011-06-25
  • 1970-01-01
  • 2023-04-02
  • 2013-06-30
  • 1970-01-01
相关资源
最近更新 更多