无法创建对象

时间:2013-03-31 01:18:30

标签: java object

我正在尝试在我的应用程序的main方法中创建一个employee类的对象,但它无法找到变量department lastName firstName或salary,我不知道为什么这就是我的语句的样子

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

2 个答案:

答案 0 :(得分:3)

  

我正在尝试在main中创建一个employee类的对象   我的应用程序的方法,但它找不到变量

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

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

答案 1 :(得分:1)

您正在将4个变量传递给构造函数,但尚未创建它们。如果你没有在很多不同的地方使用相同的名字或者某些东西,那么保持它们会更加容易。例如,如果你让这个类看起来像这样。

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
    }
}

然后在你的主要功能中包括

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

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

您不必使用this.department来区分具有相同名称的变量,很容易看出哪个变量被引用,因为它们各自具有不同的名称以及何时您的应用无法找到department,firstName,lastName和salary,你知道它不是指employeeForpartment等。