package Ch01;
import java.util.TreeSet;
public class Employee {
private String name;
private Double salary;
public Employee(String Name,Double salary){
this.name = Name;
this.salary = salary;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}public Double getSalary() {
return salary;
}public void setSalary(Double salary) {
this.salary = salary;
}
public int compareTo(Employee e){
if(this.salary>e.salary)
return -1;
if(this.salary<e.salary)
return 1;
return 0;
}
public static void main(String[] args) {
TreeSet<Employee> treeset = new TreeSet<Employee>();
treeset.add(new Employee("张三",8000.0));
treeset.add(new Employee("王五",7000.0));
treeset.add(new Employee("马六",7500.0));
for (Employee e:treeset){
System.out.println(e);
}
}
}
------最佳解决方案--------------------------------------------------------
你没有实现Comparable接口啊,你只是写了compareTo()方法.
这样写:
public class Employee implements Comparable<Employee> {}