ここにメインクラスEmployee.java
と、メインクラスを拡張する2つのクラスがあります。 ArrayList<Employee>
には、createFixedEmployee.java
とcreatePerHourEmployee
のオブジェクトが含まれています。給与でArrayList
のオブジェクトをソートする方法と、同じ給与を受け取ったオブジェクトの中に名前をアルファベット順にソートする方法がありますか?拡張クラスのArrayListを並べ替える方法は?
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();
}
}
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:
私は、私がここでエラーCannot resolve method getMonthSalary();
がコードのget Comparator.comparing();
を使用しようとしたが、動作しません。 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
}
}
ので、私はそれが正常に動作しますが、私は、すべてのクラスの1クラスではないだけをソートする必要がある
Employee.sort(Comparator.comparing(createFixedEmployee::getMonthSalary).
thenComparing(createFixedEmployee::getEmployeeName));
を使用しようとしました。 main
で
@ムレイニク:私はそれを実感し、同時に回答を掲載しました。 :-) –
@ T.J.Crowder、Mureinik確かに。私はこの問題を見つけることができませんでした..私はそれを書き留めます、ありがとうございます – phen0men