0

アプリのメイン メソッドで従業員クラスのオブジェクトを作成しようとしていますが、部門の lastName firstName または給与の変数が見つかりません。なぜこれが私のステートメントのようになるのかわかりません

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

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 を使用する必要はありません。それぞれに異なる名前が付けられているため、どの変数が参照されているかを簡単に確認できます。アプリが部門を見つけられない場合、firstName 、lastName、およびsalaryは、employeeDepartmentなどを参照していないことがわかります.

于 2013-03-31T01:51:58.440 に答える