Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用数组和多态性?_Java_Arrays_Sorting_Methods_Polymorphism - Fatal编程技术网

Java 如何使用数组和多态性?

Java 如何使用数组和多态性?,java,arrays,sorting,methods,polymorphism,Java,Arrays,Sorting,Methods,Polymorphism,假设我有一个抽象的Employee类,3个从 Employee(Commission, Salary, and Hourly) 员工包括 name employeeNumber gender 它下面的内容包括getHourlyWage或getSalary。然后,我有一个驱动程序类,它的菜单上有供用户选择的选项。最后,我有一个员工经理课程 在Driver类中,调用一个方法,按名称对所有员工进行排序,如下所示: EmployeeManager em = new EmployeeManager()

假设我有一个抽象的Employee类,3个从

Employee(Commission, Salary, and Hourly)
员工包括

name
employeeNumber
gender
它下面的内容包括
getHourlyWage
getSalary
。然后,我有一个驱动程序类,它的菜单上有供用户选择的选项。最后,我有一个
员工经理
课程

在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.
非常感谢您的帮助


我想我对员工、工资、小时工资、佣金和司机的要求都是正确的。我只是不知道如何将他们与这位员工经理联系起来。

我想你会发现这个链接很有用:


此处显示的示例使用Employee类和列表而不是数组。使用comparator和comparable解决排序问题。

我想您会发现此链接很有用:


此处显示的示例使用Employee类和列表而不是数组。排序是使用comparator和comparable解决的。

所有对象
佣金、工资和小时工资
都扩展了超类
员工
,因此将它们添加到数组中时非常简单:

/**
 * 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; 
}
此方法将起作用,因为所有子类
commission
Salary
Hourly
都可以泛化为
Employee
超类对象

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

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

public void add(Employee e)
{
   employees.add(e);
}
List employees=new ArrayList();
公共作废添加(员工e)
{
增加(e);
}
对于排序,您应该让
Employee
类实现
java.util.compariable
接口并重写
comparieto(objecto)
方法


希望这能有所帮助。

所有对象
佣金、工资和小时工资
都扩展了超类
员工
,因此,将它们添加到数组中时非常简单:

/**
 * 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; 
}
此方法将起作用,因为所有子类
commission
Salary
Hourly
都可以泛化为
Employee
超类对象

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

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

public void add(Employee e)
{
   employees.add(e);
}
List employees=new ArrayList();
公共作废添加(员工e)
{
增加(e);
}
对于排序,您应该让
Employee
类实现
java.util.compariable
接口并重写
comparieto(objecto)
方法


希望这有帮助。

我假设您希望使用数组而不是集合框架

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

它看起来像这样

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

我假设您希望使用数组而不是集合框架

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

它看起来像这样

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

请展示你已经做了什么,并提出一个具体的问题。答案是否有用?0反馈很难确定您的问题。请说明您已经做了什么,并提出具体问题。答案是否有用?很难通过0反馈确定您的问题。