如何在 Java 中为子类的变量分配特定编号?

问题描述 投票:0回答:1

主要方法 导入 java.util.Scanner;

公开课 HW4 {

public static int idScan() {
    Scanner x = new Scanner(System.in);
    int id = x.nextInt();
    return id;
}

public static double salaryScan() {
    Scanner y = new Scanner(System.in);
    double salary = y.nextInt();
    return salary;
}

public static int statusScan() {
    Scanner z = new Scanner(System.in);
    int status = z.nextInt();
    return status;
}

public static void main(String args[]) {
    
    Employee [] employee = new Employee[10];    
    employee [0] = new Employee(1, 0, 0);   
    employee [1] = new Employee(1, 0, 0);   
    employee [2] = new Employee(1, 0, 0);   
    employee [3] = new Employee(1, 0, 0);   
    employee [4] = new Employee(1, 0, 0);   
    employee [5] = new Employee(1, 0, 0);   
    employee [6] = new Employee(1, 0, 0);   
    employee [7] = new Employee(1, 0, 0);   
    employee [8] = new Employee(1, 0, 0);   
    employee [9] = new Employee(1, 0, 0);   
    
    for(Employee e : employee) {    
        System.out.println("Enter emplpoyee ID of an employee?");
        int id = idScan();
        System.out.println("Enter salary of the employee?");
        double sal = salaryScan();
        System.out.println("Is this employee a manager or worker? (Enter 1 for worker and 2 for manager)");
        int stat = statusScan();
    }

}

}

Employee.java 公开课员工{

private int employeeID;
private double salary;
private int status;

//Constructor
public Employee(int ID, double esalary, int estat) {
    employeeID = ID;
    esalary = salary;
    status = estat;
}

//Getter ID
public int getID() {
    return employeeID;
}
//Setter ID
public void setID(int ID) {
    employeeID = ID;
}

//Getter Salary
public double getSalary() {
    return salary;
}
//Setter Salary
public void setSalary(double esalary) {
    salary = esalary;
}

//Getter Status
public double getStatus() {
    return status;
}
//Setter Status
public void setStatus(int estat) {
    status = estat;
}

}

ManagerEmployee.java 公共类 ManagerEmployee 扩展 Employee {

//Super Constructor
public ManagerEmployee(int ID, double esalary, int estat) {
    super(ID, esalary, estat = 1);
}   

}

WorkerEmployee.java 公共类 WorkerEmployee 扩展 Employee {

//Super Constructor
public WorkerEmployee(int ID, double esalary, int estat) {
    super(ID, esalary, estat = 2);
}

}

我正在做的程序涉及通过扫描仪输入计算两种类型员工(经理/工人)的员工工资。我很困惑的一个部分涉及到我需要通过 for 循环中的扫描仪输入一个数字来指示工人或经理(1 代表工人,2 代表经理)的部分。我通常对需要在哪里建立该规则感到困惑,无论是在 Employee 类中还是在两个子类中。

java class parent-child
1个回答
1
投票

我认为你正在尝试实现工厂模式。在这种情况下,您可以将

Employee
类设为
abstract
。因为不会有
Employee
,而只是一个
Employee
。它必须是
Manager
Worker
。当您使用多态性时,不需要将
status
保留在
Employee
类中。您可以通过其类型找到该员工的类型。所以,你的类层次结构就变成了

Employee.java

public abstract class Employee {

    private int employeeID;
    private double salary;

    //Constructor
    public Employee(int ID, double esalary) {
        employeeID = ID;
        salary = esalary;
    }

    //Getter ID
    public int getID() {
        return employeeID;
    }

    //Setter ID
    public void setID(int ID) {
        employeeID = ID;
    }

    //Getter Salary
    public double getSalary() {
        return salary;
    }

    //Setter Salary
    public void setSalary(double esalary) {
        salary = esalary;
    }
}

ManagerEmployee.java

public class ManagerEmployee extends Employee {

    //Super Constructor
    public ManagerEmployee(int ID, double esalary) {
        super(ID, esalary);
    }

}

WorkerEmployee.java

public class WorkerEmployee extends Employee {

    //Super Constructor
    public WorkerEmployee(int ID, double esalary) {
        super(ID, esalary);
    }

}

在 main 方法中,您应该在检查用户输入后创建特定员工。

HW4.java

import java.util.Scanner;

public class HW4 {

    public static int idScan() {
        Scanner x = new Scanner(System.in);
        int id = x.nextInt();
        return id;
    }

    public static double salaryScan() {
        Scanner y = new Scanner(System.in);
        double salary = y.nextInt();
        return salary;
    }

    public static int statusScan() {
        Scanner z = new Scanner(System.in);
        int status = z.nextInt();
        return status;
    }

    public static void main(String args[]) {
        int numOfEmployees = 10;
        Employee[] employee = new Employee[numOfEmployees];

        for (int i = 0; i < numOfEmployees; i++) {
            System.out.println("Enter employee ID of an employee?");
            int id = idScan();
            System.out.println("Enter salary of the employee?");
            double sal = salaryScan();
            System.out.println("Is this employee a manager or worker? (Enter 1 for worker and 2 for manager)");
            int stat = statusScan();
            if (stat == 1) {
                employee[i] = new WorkerEmployee(id, sal);
            } else if (stat == 2) {
                employee[i] = new ManagerEmployee(id, sal);
            } else {
                // handle error
            }
        }
    }
}

或者,我们可以使用

enum
并删除子类,因为它们不会向
Employee
类添加任何属性/功能。那样的话

Employee.java

public class Employee {
    public enum Status {
        MANAGER,
        WORKER
    }

    private int employeeID;
    private double salary;

    private Status status;

    //Constructor
    public Employee(int ID, double esalary, Status stat) {
        employeeID = ID;
        salary = esalary;
        status = stat;
    }

    //Getter ID
    public int getID() {
        return employeeID;
    }

    //Setter ID
    public void setID(int ID) {
        employeeID = ID;
    }

    //Getter Salary
    public double getSalary() {
        return salary;
    }

    //Setter Salary
    public void setSalary(double esalary) {
        salary = esalary;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }
}

你的

main
方法将变成

    public static void main(String args[]) {
        int numOfEmployees = 10;
        Employee[] employee = new Employee[numOfEmployees];

        for (int i = 0; i < numOfEmployees; i++) {
            System.out.println("Enter employee ID of an employee?");
            int id = idScan();
            System.out.println("Enter salary of the employee?");
            double sal = salaryScan();
            System.out.println("Is this employee a manager or worker? (Enter 1 for worker and 2 for manager)");
            int stat = statusScan();
            if (stat == 1) {
                employee[i] = new Employee(id, sal, Employee.Status.WORKER);
            } else if (stat == 2) {
                employee[i] = new Employee(id, sal, Employee.Status.MANAGER);
            } else {
                // handle error
            }
        }
    }

它与您当前的方法类似,但使用

enum
代替
int
,以便任何任意值都不能分配为
status

© www.soinside.com 2019 - 2024. All rights reserved.