【问题标题】:How to use arrays along with polymorphism?如何将数组与多态一起使用?
【发布时间】:2012-02-07 22:35:48
【问题描述】:

好吧,假设我有一个抽象的 Employee 类,3 个类继承自

Employee(Commission, Salary, and Hourly)

员工包括

name
employeeNumber
gender

它下面的内容包括getHourlyWagegetSalary。然后我有一个带有菜单的驱动程序类,其中包含供用户选择的选项。最后我有一个EmployeeManager 类。

在 Driver 类中,按名称对所有员工进行排序的方法之一是这样调用的:

EmployeeManager em = new EmployeeManager();
em.sortName(); 

现在我在 EmployeeManager 类中做什么? 首先我制作了数组:

private Employee[] employees = new Employee[100];

那么我将如何对它进行排序,或者首先我如何使用调用的方法添加员工:

addEmployee(int si, String first, char g, double amount)

int    si    =   1 if the employee is a salary,
                 2 if the employee is hourly, 
                 3 if commission
string first =   name
char   g     =   gender
double amount=   wage, salary, or rate-depends on what kind of employee.

您的帮助将不胜感激!!

我认为我的员工、薪水、小时工、佣金和司机都是正确的。我只是不知道如何将它们与这个 EmployeeManager 联系起来。

【问题讨论】:

  • 请展示您已经完成的工作并提出具体问题。
  • 这些答案是否有用? 0 反馈很难确定您的问题。

标签: java arrays sorting methods polymorphism


【解决方案1】:

Commission, Salary, and Hourly 的所有对象都扩展了超类Employee,因此将它们添加到数组时就像这样简单:

/**
 * Adds an Employee object to the employees 
 * array at index i. Note: this method doesn't
 * check if there is already an Employee at index i
 *
 * List item
 * @param index The index to add the Employee object at
 * @param e     The Employee object to add
 */
 public void addEmployee(int index, Employee e)
{
    employees[i] = e; 
}

此方法将起作用,因为所有子类 ComissionSalaryHourly 都可以泛化为 Employee 超类对象。

使用ArrayList<Employee> employees 会容易得多,然后您可以完全忽略索引,只需使用ArrayList 类中的add(E e) 方法将Employee 对象附加到列表中。

List<Employee> employees = new ArrayList<>();

public void add(Employee e)
{
   employees.add(e);
}

对于排序,您应该让Employee 类实现java.util.Comparable 接口并覆盖compareTo(Object o) 方法。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我想你会发现这个链接很有帮助:Java Sorting: Comparator vs comparable

    此处显示的示例使用 Employee 类和 List 而不是数组。排序是使用比较器和可比性解决的。

    【讨论】:

      【解决方案3】:

      我假设您想使用数组而不是 Collections 框架。

      您需要遍历员工数组并将每个数组设置为新员工。

      看起来像这样

       for (int i = 0; i < employees.length; i++) {
            employees[i] = new Employee();
       }
      

      然后,您可以继续添加员工

       public void addEmployee(int si, String first, char g, double amount) {
           if (numberOfEmployees < employees.length) {
                employees[numberOfEmployees++] = new Employee(si, first, g, amount);
           } else {
                //double the size of the array...
           }
        }
      

      鉴于,您的 Employee 类还需要这样的构造函数,即:

       . . .
       Employee(int si, String first, char g, int amount) {
           this.si = si;
           this.first = first;
           this.g = g;
           this.amount = amount;
       }
      
       Employee() {
           //empty constructor..
       }
       . . .
      

      【讨论】:

        猜你喜欢
        • 2014-12-01
        • 2017-04-23
        • 2021-03-08
        • 2016-12-20
        • 2014-03-06
        • 1970-01-01
        • 2010-09-14
        • 2012-01-20
        • 2020-12-04
        相关资源
        最近更新 更多