如何对扩展类的ArrayList进行排序?

时间:2016-06-26 07:26:09

标签: java class sorting arraylist compiler-errors

这里是一个主类Employee.java和另外两个扩展主类的类。 ArrayList<Employee>包含来自createFixedEmployee.javacreatePerHourEmployee的对象。如何按工资对ArrayList中的对象进行排序,如果某些对象获得相同的工资,则按字母顺序对其名称进行排序?

我尝试使用Comparator.comparing();但不起作用,我收到错误Cannot resolve method getMonthSalary();这是一段代码:

createPerHourEmployee.java

public class createPerHourEmployee extends Employee {
    private double salary;

    public createPerHourEmployee() {}

    public double getSalaryPerHour() { return this.salary; }

    public double setSalaryPerHour(double value) {
        return this.salary = value;
    }


    public createPerHourEmployee (int _id, String _name, double _salary) {
        setEmployeeID(_id);
        setEmployeeName(_name);
        this.salary = _salary;
    }

    public double getMonthSalary() {
        return salary * (20 * 0.8);
    }

    public String toString() {
        return getEmployeeID() + ", " + getEmployeeName() + ", " + getSalaryPerHour();
    }
}

createFixedEmployee.java

public class createFixedEmployee extends Employee {
    private double salary;

    public createFixedEmployee() {}

    public double getSalaryFixed() {
        return this.salary;
    }

    public double setSalaryFixed(double value) {
        return this.salary = value;
    }

    public createFixedEmployee(int _id, String _name, double _salary) {
        setEmployeeID(_id);
        setEmployeeName(_name);
        this.salary = _salary;
    }

    public double getMonthSalary() {
        return this.salary;
    }

    public String toString() {
        return getEmployeeID() + ", " + getEmployeeName() + ", " + getSalaryFixed();
    }

}

Employee.java

import java.util.ArrayList;
import java.util.Comparator;

public abstract class Employee {
    private int base_id;
    private String base_name;

    public int getEmployeeID () {
        return this.base_id;
    }

    public int setEmployeeID (int value) {
        return this.base_id = value;
    }

    public String getEmployeeName () {
        return this.base_name;
    }

    public String setEmployeeName(String value) {
        return this.base_name = value;
    }

    public abstract double getMonthSalary();

    public static void main(String[] argc) {
        ArrayList<Employee> Employee = new ArrayList<Employee>();
        Employee.add(new createPerHourEmployee(1, "asd", 1300));
        Employee.add(new createFixedEmployee(7, "asds", 14025));
        Employee.add(new createPerHourEmployee(2, "nikan", 1230));
        Employee.add(new createPerHourEmployee(3, "nikalo", 12330));
        Employee.add(new createFixedEmployee(6, "aaaa", 14025));
        Employee.add(new createFixedEmployee(4, "nikaq", 140210));
        Employee.add(new createFixedEmployee(5, "nikas", 124000));
        Employee.add(new createFixedEmployee(6, "nikab", 14025));


        Employee.sort(Comparator.comparing(Employee::getMonthSalary)
              .thenComparing(Employee::getEmployeeName)); // here is an error

    }
}

所以我尝试使用

Employee.sort(Comparator.comparing(createFixedEmployee::getMonthSalary).
                thenComparing(createFixedEmployee::getEmployeeName));

它工作正常,但我需要为所有类排序,而不仅仅是一个类。

2 个答案:

答案 0 :(得分:2)

排序List<T>时,您绝对可以使用任何T祖先方法的功能参考。这里的问题是您给列表的名称 - 您已经命名了本地变量Employee,当您尝试在该变量中创建函数引用时,它隐藏了类型Employee的名称。 Comparator。只要给它一个更好的名字,你应该没事。 E.g:

ArrayList<Employee> employeeList = new ArrayList<Employee>();
// Or better yet: List<Employee> employeeList = new ArrayList<>();

employeeList.add(new createPerHourEmployee(1, "asd", 1300));
employeeList.add(new createFixedEmployee(7, "asds", 14025));
employeeList.add(new createPerHourEmployee(2, "nikan", 1230));
employeeList.add(new createPerHourEmployee(3, "nikalo", 12330));
employeeList.add(new createFixedEmployee(6, "aaaa", 14025));
employeeList.add(new createFixedEmployee(4, "nikaq", 140210));
employeeList.add(new createFixedEmployee(5, "nikas", 124000));
Employee.add(new createFixedEmployee(6, "nikab", 14025));


employeeList.sort(Comparator.comparing(Employee::getMonthSalary)
      .thenComparing(Employee::getEmployeeName));

答案 1 :(得分:1)

main中,您已将本地变量命名为Employee。这会影响Employee类的标识符。变量名不应该以大写字母开头, 在引用数组或列表时应该是复数(除非它们以“list”或类似名称结尾),理想情况下不应该是与任何班级名称相同。

所以将变量更改为employees。修复此问题,以及使用Employee::getMonthSalary的版本以及此类工作:

employees.sort(Comparator.comparing(Employee::getMonthSalary)
      .thenComparing(Employee::getEmployeeName));

我还会将createPerHourEmployee更改为PerHourEmployee,因为Java中压倒性的约定是类名以大写字母开头并且是名词,而不是动词。同样,createFixedEmployee也会(比方说)SalariedEmployee