一、创建接口文件 IComparable
package Interface;
public interface IComparable <T> extends Comparable<T>{
public int comparableto(T obj);
}
二、实现接口类型 Emploee
package Interface;
public class Emploee implements IComparable<Emploee> {
private String name;
private double salary;
public Emploee(String name, double salary)
{
this.name = name;
this.salary = salary;
}
public String getName()
{
return name;
}
public double getSalary(){
return salary;
}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent /100;
salary += raise;
}
@Override
public int comparableto(Emploee obj) {
return Double.compare(salary,obj.salary);
}
@Override
public int compareTo(Emploee o) {
return Double.compare(salary,o.salary);
}
}
三、测试类
package Interface;
import java.util.*;
public class EmployeeSortTest {
public static void main(String[] args) {
Emploee[] elyArr = new Emploee[3];
elyArr[0] = new Emploee("刘老师",35000);
elyArr[1] = new Emploee("徐老师",75000);
elyArr[2] = new Emploee("梦老师",38000);
Arrays.sort(elyArr);
for(Emploee e:elyArr)
{
System.out.println("name" +e.getName() + ",salary = " +e.getSalary());
}
}
}
结果