You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中如何根据输入标识为子类指定状态变量值?

问题解决思路与代码修正

现有代码问题梳理

  • Employee构造函数赋值错误:esalary = salary 赋值顺序颠倒,应改为 salary = esalary
  • getStatus返回类型错误:返回的是int类型的status,但方法声明为double
  • 子类构造函数冗余:经理和工人的状态是固定属性,无需在构造参数中接收estat,直接给父类传固定值即可
  • 主方法逻辑缺失:仅完成输入操作,未根据输入的员工类型创建对应子类对象并赋值

修正后的类结构

Employee.java(父类)

public class Employee {
    private int employeeID;
    private double salary;
    private int status; // 1=工人,2=经理,由子类固定赋值

    // 构造函数
    public Employee(int ID, double esalary, int estat) {
        employeeID = ID;
        salary = esalary; // 修正赋值顺序
        status = estat;
    }

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

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

    public double getSalary() {
        return salary;
    }

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

    public int getStatus() { // 修正返回类型为int
        return status;
    }

    public void setStatus(int estat) {
        status = estat;
    }
}

ManagerEmployee.java(子类)

public class ManagerEmployee extends Employee {
    // 经理状态固定为2,无需接收status参数
    public ManagerEmployee(int ID, double esalary) {
        super(ID, esalary, 2);
    }
}

WorkerEmployee.java(子类)

public class WorkerEmployee extends Employee {
    // 工人状态固定为1,无需接收status参数
    public WorkerEmployee(int ID, double esalary) {
        super(ID, esalary, 1);
    }
}

主方法(HW4.java)逻辑修正

主方法根据输入的stat值创建对应子类对象,存入数组:

import java.util.Scanner;

public class HW4 {
    // 复用同一个Scanner,避免多次创建资源
    private static final Scanner scanner = new Scanner(System.in);

    public static int idScan() {
        System.out.println("Enter employee ID of an employee?");
        return scanner.nextInt();
    }

    public static double salaryScan() {
        System.out.println("Enter salary of the employee?");
        return scanner.nextDouble(); // 用nextDouble接收薪资,避免int转double的精度问题
    }

    public static int statusScan() {
        System.out.println("Is this employee a manager or worker? (Enter 1 for worker and 2 for manager)");
        return scanner.nextInt();
    }

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

        for (int i = 0; i < employees.length; i++) {
            int id = idScan();
            double sal = salaryScan();
            int stat = statusScan();

            // 根据输入状态创建对应子类对象
            if (stat == 1) {
                employees[i] = new WorkerEmployee(id, sal);
            } else if (stat == 2) {
                employees[i] = new ManagerEmployee(id, sal);
            } else {
                System.out.println("Invalid status, defaulting to worker");
                employees[i] = new WorkerEmployee(id, sal);
            }
        }

        // 可选:验证输入结果
        System.out.println("\nEmployee List:");
        for (Employee e : employees) {
            String type = e.getStatus() == 1 ? "Worker" : "Manager";
            System.out.printf("ID: %d, Salary: %.2f, Type: %s%n", e.getID(), e.getSalary(), type);
        }

        scanner.close(); // 关闭Scanner释放资源
    }
}

核心逻辑说明

  1. 状态变量归属:status放在父类Employee中,作为所有员工的共同属性;子类的状态是固定的,因此子类构造时直接给父类传入固定值,无需外部传入。
  2. 类型区分逻辑:主方法通过输入的stat值判断创建WorkerEmployee还是ManagerEmployee对象,利用多态特性存入Employee数组。
  3. 资源优化:复用同一个Scanner对象,避免多次创建资源浪费,使用完毕后关闭。

内容的提问来源于stack exchange,提问作者Erik Sulin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 11:31:11