public class Employee implements Comparable<Employee>
{
public Employee(String n,double s)
{
name = n;
salary = s;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public void raiseSalary(double byPercent)
{
double raise = salary*byPercent/100;
salary += raise;
}
//接口
public int compareTo(Employee other)
{
if(salary < other.salary)return -1;
if(salary > other.salary)return 1;
return 0;
}
private String name;
private double salary;
}
import java.util.Arrays;
public class ExampleSortTest
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
Employee[] staff = new Employee[3];
staff[0] = new Employee("xiong",1300);
staff[1] = new Employee("juan",1120);
staff[2] = new Employee("xinfang",1200);
Arrays.sort(staff);
for(Employee e : staff)
{
System.out.println("name = " + e.getName() + " salary= " + e.getSalary());
}
}
}