员工继承,多态和抽象类

时间:2012-02-07 03:58:31

标签: java arrays polymorphism

我必须制作一个涉及6个课程的程序。

  1. abstract Employee class-superclass
  2. 薪资员工 - 继承自 员工
  3. HOurlyEmployee-继承自员工
  4. 佣金 员工 - 继承员工
  5. employeeManager包含的数组 员工并提供管理阵列的权限 存储在
  6. 中的员工
  7. employeeDriver - 包含main方法和a 菜单系统供用户使用。
  8. 所以我认为除了EmployeeManager之外我得到了所有这些。在本课程中,我知道我需要使用EmployeeDriver中的方法。我不确定如何处理数组,因为我需要输入SalaryEmployeeHourlyEmployeeCommissionEmployee的信息。用户还可以选择列出所有HOurlyEmployees或SalaryEmployees或commisionEmployees及其信息。我知道我必须遍历数组,但我不知道该怎么做。请帮助我!

1 个答案:

答案 0 :(得分:0)

这是一个开始:

import java.math.BigDecimal;
import java.util.*;
abstract class Employee {
    Employee(String name) {
        this.name = name;
    }
    final String name;
}
class SalariedEmployee extends Employee {
    SalariedEmployee(String name, BigDecimal salary) {
        super(name);
        this.salary = salary;
    }
    public String toString() {
        return getClass().getSimpleName() + " " + name + " " + salary;
    }
    final BigDecimal salary;
}
class HourlyEmployee extends Employee {
    HourlyEmployee(String name, BigDecimal hourlyRate) {
        super(name);
        this.hourlyRate = hourlyRate;
    }
    public String toString() {
        return getClass().getSimpleName() + " " + name + " " + hourlyRate;
    }
    final BigDecimal hourlyRate;
}
class CommissionEmployee extends Employee {
    CommissionEmployee(String name, BigDecimal percentage) {
        super(name);
        this.percentage = percentage;
    }
    public String toString() {
        return getClass().getSimpleName() + " " + name + " " + percentage + "%";
    }
    final BigDecimal percentage;
}
class Manager {
    public String toString() {
        return Arrays.asList(employees).toString();
    }
    Employee[] employees = new Employee[] { new SalariedEmployee("joe", BigDecimal.valueOf(4567.89)),
            new HourlyEmployee("mary", BigDecimal.valueOf(12.34)),
            new CommissionEmployee("bob", BigDecimal.valueOf(12.34)) };
}
public class Main {
    public static void main(String[] args) {
        System.out.println(new Manager());
    }
}