public class EmployeeSortTest
{
public static void main(String[] args)
{
Employee[] staff=new Employee[3];
staff[0]=new Employee("Harry Hacker",35000);
staff[1]=new Employee("Carl Crarker",75000);
staff[2]=new Employee("Tony Tester",38000);
Arrays.sort(staff);
for(Employee e:staff)
System.out.println("name"=+e.getName()+",salary="+e.getSalary());//=号放在""里面
}
}
class Employee implements Comparable<Employee>
{
double salary ;//要定义在前面。
public Employee(String n, double s )
{
name=n;
salary=s;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary(); //salary(),取值是不用()的。
}
public void raiseSalary(double byPercent)
{
double raisa=salary*byPercent/100;
salary+=raisa;
}
public int compareTo(Employee other)
{
if(salary<other.salary)return -1;
if(salary>other.salary)return 1;
return 0;
}
private String name;
private double saalary; //要定义在前面,否则会出错。
} |