0

我正在尝试在我的应用程序的主要方法中创建员工类的对象,但它找不到变量部门姓氏名字或薪水,我不确定为什么这是我的陈述的样子

Employee employee = new Employee( department, lastName, firstName, salary);

员工类看起来像

package javaapplication14;
public class Employee implements DepartmentConstants, Displayable 
{
    private int department;
    private String firstName;
    private String lastName;
    private double salary;

    public Employee(int department, String lastName, String firstName,
        double salary)
    {
        this.department = department;
        this.lastName = lastName;
        this.firstName = firstName;
        this.salary = salary;
    }

    @Override
    public String getDisplayText() 
    {
        String displayText = firstName + lastName + salary + department ;
        return displayText;
    }
}

我不确定是否需要展示其实现的接口,但如果需要,我将编辑我的帖子以包含它们。主要方法是

package javaapplication14;
public class DisplayableTestApp
{
    public static void main(String args[])
    {
        System.out.println("Welcome to the Displayable Test application\n");

        // create an Employee object
        Employee employee = new Employee( department, lastName, firstName, salary);

        // display the employee information
        String displayTextEmployee = employee.getDisplayText();
        System.out.println(displayTextEmployee);

        // create a Product object
        Product product = new Product();
        // display the product information
        String displayText = product.getDisplayText();
        System.out.println(displayText);
    }
}
4

2 回答 2

3

我正在尝试在我的应用程序的主要方法中创建员工类的对象,但它找不到变量

它找不到变量,因为它们不存在于您的构造函数调用的范围内。您必须创建变量并将其传递给构造函数。

该类Employee接受构造函数:

public Employee(int department, String lastName, String firstName, double salary)

所以,举个例子:

Employee employee = new Employee(1, "Smith", "John", 50000);

或者:

int department = 1;
String lastName = "Smith";
String firstName = "John";
double salary = 50000;
Employee employee = new Employee(department, lastName, firstName, salary);
于 2013-03-31T01:23:34.750 回答
1

Your are passing the 4 variables into the constructor but you haven't created them yet. It would be a lot easier to keep them straight if you didn't use the same name or something close in so many different places. For instance if you made the class look like this.

public class Employee implements Department Constants, Displayable
{
    private int employeeDepartmet;
    private String employeeFirstName;
    private String employeeLastName;
    private double employeeSalary;

    public Employee (int dept, String firstN, String lastN, double pay)
    {
        employeeDepartment = dept;
        employeeFirstName = firstN;
        employeeLastName = lastN;
        employeeSalary = pay;
    }

@Override
public getDisplayText ()
    {
        String displayText = employeeDepartment + employeeFirstName + employeeLastName + employeeSalary;
        return displayText
    }
}

Then in your main function include

int department = 3;
String firstName = "Joe";
String lastName = "Adams";
double salary = 7.45;

Employee employeeOne = new Employee(department, firstName, lastName, salary);

You don't have to use this.department to distinguish between the variables that have the same name, it is easy to see which variable is being referred to since they each have a different name and when your app can't find department, firstName, and lastName, and salary, you know that it isn't referring to employeeDepartment etc.

于 2013-03-31T01:51:58.440 回答