package zzy;
import static java.lang.System.*;
import java.util.*;
/**
*
* @author Zhu Zhengyan
*/
public class zzy8 {
public static void main(String[] args)
{
Emp[] staff = new Emp[3];
staff[0] = new Emp("zzy",35000);
staff[1] = new Emp("zkb",75000);
staff[2] = new Emp("lt",48000);
Arrays.sort(staff);
for(Emp e:staff)
out.println("name = "+e.getName()+",salary = "+e.getSalary());
}
}
class Emp implements Comparable<Emp>
{
private String name;
private double salary;
public Emp(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;
}
public int compareTo(Emp a)
{
return Double.compare(salary, a.salary);
//Double.compare方法,如果第一个参数小于第二个参数,返回一个负值,相等返回0,否则返回正值
}
}
相关文章
- Comparator接口,类数组排序
- [Java]有关比较器(Comparator接口)的用法及对象数组排序问题
- 第十一章《Java实战常用类》第9节:Comparable接口和Comparator接口
- Java的Comparator接口实现自定义排序
- JAVA 使用Comparator接口实现自定义排序
- 数组自定义排序:IComparable和IComparer接口
- 用Comparator接口自定义排序
- java基础知识回顾之---java String final类普通方法的应用之字符串数组排序
- 集合工具类Collections指南,以及Comparable和Comparator排序详解
- java数组排序Arrays.sort,以及结合Comparator接口的用法