-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeeSortTest.java
More file actions
74 lines (66 loc) · 2.38 KB
/
EmployeeSortTest.java
File metadata and controls
74 lines (66 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package ds;
import java.util.*;
public class EmployeeSortTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee[] staff = new Employee[3];
staff[0] = new Employee("harry Hacker",35000);
staff[1] = new Employee("carl cracke",75000);
staff[2] = new Employee("tony Tester",38000);
Arrays.sort(staff);//sort��������ʵ�ֶԶ������������DZ���ʵ�� Comparable�ӿ�
/*Comparable�ӿ�ԭ��Ϊ��
* public interface Comparable<T>
* {
* int compareTo��T other);//�ӿڵ��з����Զ�����public����
* }
*/
for(Employee e: staff)
System.out.println("id=" + e.getId() + " name=" + e.getName()+
".salary=" + e.getSalary());
}
}
/*
* ��ΪҪʵ�ֶ�Employee���������������Employee����Ҫʵ��Comparable�ӿڣ�
* Ҳ����Ҫʵ��comepareTo()����
*/
class Employee implements Comparable<Employee>
{
public Employee(String n, double s)
{
name = n;
salary = s;
Random ID = new Random();
id = ID.nextInt(10000000);
}
public int getId()
{
return id;
}
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(id<other.id)//����Ƚϵ���ʲô sort����ʵ�ֵľ��ǰ��մ˱ȽϵĶ�����С��������
return -1;
if(id>other.id)
return 1;
return 0;
}
private int id;
private String name;
private double salary;
}