作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是一个主类 Employee.java
和另外 2 个扩展主类的类。 ArrayList<Employee>
包含来自 createFixedEmployee.java
的对象和 createPerHourEmployee
.如何对 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();
}
}
员工.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));
它工作正常,但我需要对所有类进行排序,而不仅仅是一个类。
最佳答案
排序 List<T>
时,您绝对可以使用对任何 T
的功能引用祖传之法。这里的问题是您给列表的名称 - 您已将局部变量命名为 Employee
, 它隐藏了类型的名称 Employee
当您尝试在 Comparator
中创建函数引用时.给它一个更好的名字,你应该没问题。例如:
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));
关于java - 如何对扩展类的 ArrayList 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38036156/
我是一名优秀的程序员,十分优秀!