简体   繁体   English

如何覆盖 compareto 方法来订购员工的薪水?

[英]How can I override the compareto method to order Employees's salary?

I have 2 classes: person and employee and an interface Human Within my Person class, I have a compareTo Method (Human h) Which assigns the +1,-1 and 0 for the person's age.我有 2 个类:人员和雇员以及一个接口 Human 在我的 Person class 中,我有一个 compareTo 方法(Human h),它为这个人的年龄分配 +1、-1 和 0。 My class employee = public class Employee extends Person implements Human = I have a compareTo method as well, which needs to account for the employees salary if the age is the same (for sorting).我的 class employee = public class Employee extends Person implements Human = 我也有一个 compareTo 方法,如果年龄相同(用于排序),则需要考虑员工工资。

I am not quite sure how to tackle this?我不太确定如何解决这个问题? I was able to make the compreTo for the Persons class but I am not sure how to have both person and employe sorted here.我能够为 Persons class 制作 compreTo,但我不确定如何在此处对人员和雇员进行排序。

Thank you for the help.感谢您的帮助。

I have already tried this in my Employee class:我已经在我的员工 class 中尝试过这个:

compareTo (Human h) {
Employee e = (Employee)h;

if (super.compareTo(h) == 0 && getSalary ()< e.getSalary())
    return -1;
    else if (super.compareTo(h) == 0 && getSalary () == e.getSalary())
        return 0;
    else 
        return 1;

}

This one works, but I want to be able to use instanceof to solve this problem:这个可行,但我希望能够使用 instanceof 来解决这个问题:

     public int compareTo(Human h) {

    // TODO Auto-generated method stub

     if (getAge() < h.getAge()) {
          return -1;
        } else if (getAge() > h.getAge()) {
          return 1;
        } else {
            Employee e = (Employee)h;
          // age is identical: compare salary
          if (getSalary() < e.getSalary()) {
            return -1;
          } else if (getSalary() > e.getSalary()) {
            return 1;
          } else {
            return 0;
          }
        }
      }

Below I had proved the amount of code I think is necessary for this question:下面我已经证明了我认为这个问题需要的代码量:

public interface Human extends Comparable <Human>{
//extends = is a


int getAge();
String getName();

}



public class Person implements Human {
    private int age; 
private String name;
    public int compareTo(Human h) {


    //System.out.println(this.age + ". " +h.getAge());
    if (h.getAge() > getAge())
        return -1;
    else if (getAge() == h.getAge())
        return 0;
    else 
        return 1;    
}

public class Employee extends Person implements Human{


private int salary;
private String employer; 

public int compareTo(Human h) {
 ???
}



  public static void main(String[] args) {
  ArrayList<Human> p = new ArrayList<Human>();
    p.add(new Person("A", 1));
    p.add(new Employee("B", 31, "E1", 45000));
    p.add(new Person("C", 122));
    p.add(new Employee("D", 3, "E2", 54321));
    p.add(new Person("E", 21));
    p.add(new Employee("F", 31, "E1", 21000));
    p.add(new Employee("G", 31, "E1", 38000));
    System.out.println(p);
    Collections.sort(p);
    System.out.println(p); }

This is what I am trying to test:这就是我要测试的内容:

non sorted: [Person:[A, 1], Employee:[B, 31][E1, 45000], Person:[C, 122], Employee:[D, 3][E2, 54321], Person:[E, 21], Employee:[F, 31][E1, 21000], Employee:[G, 31][E1, 38000]]

sorted: [Person:[A, 1], Employee:[D, 3][E2, 54321], Person:[E, 21], Employee:[F, 31][E1, 21000], Employee:[G, 31][E1, 38000], Employee:[B, 31][E1, 45000], Person:[C, 122]]

Any help would be appreciated.任何帮助,将不胜感激。

To ensure a correct ordering;确保正确订购; the compareTo method needs to satisfy the contract specified by the Comparable interface. compareTo方法需要满足Comparable接口指定的契约

Unfortunately, there is no way to extend Person ;不幸的是,没有办法扩展Person overriding compareTo in Employee to compare the salary while preserving the contract.覆盖Employee中的compareTo以在保留合同的同时比较薪水。

A simple solution is to pass a comparator to Collections.sort() ;一个简单的解决方案是将比较器传递给Collections.sort() ensuring that all the elements of the collection use the same comparator implementation:确保集合的所有元素都使用相同的比较器实现:

Comparator.comparingInt(Human::getAge).thenComparingInt(h -> h instanceof Employee ? ((Employee) h).getSalary() : 0)

You can achieve this by simply implementing the compareTo method in Person and Employee as follow:您可以通过简单地在 Person 和 Employee 中实现 compareTo 方法来实现这一点,如下所示:

// In Person class
@Override
public int compareTo(Human h) {
    return age - h.getAge();
}

And

// In Employee class:
@Override
public int compareTo(Human h) {
    int result = super.compareTo(h);
    if ((result == 0) && (h instanceof Employee)) {
        result = salary - ((Employee) h).salary;
    }
    return result;
}

Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM