错误人员无法强制转换为java.lang.Comparable

时间:2013-02-15 17:44:37

标签: java mergesort

当我在测试器类中使用数组排序功能时,我遇到了错误。

import java.util.Arrays;
public class tester implements EmployeeInfo
{

public static void main(String[] args) throws CloneNotSupportedException
{
        Employee[]emps = new Employee[9];
        double sum=0,total=0;

        emps[0]= new Staff("Chan, Scott","123",'M',1959,2,23,35.00);
        emps[1]= new Staff("Salinas, Brian","456",'F',1964,7,12,30.00);
        emps[2]= new Staff("Weir, Allen","789",'M',1970,6,2,22.00);
        emps[3]= new Faculty("Im, Lee", "243", 'F',1962,4,27, "Full","Ph.d",
                "Engineering", 3);
        emps[4]= new Faculty("Bui, Trung","791" , 'F', 1975,3,14, 
                "Associate","Ph.d","English", 1);
        emps[5] = new Faculty("Monreno, Maria", "623", 'F', 1980,5,22, 
                "Assistant","MS","Physical Education", 0);
        emps[6]= new Partime("Lee, Chesong", "455", 'F', 
                1977,8,10,35,20);
        emps[7]= new Partime("Garcia, Frank", "678", 'F', 
                1987,9,15,30,25);
        emps[8] = new Partime("Alquilo, Roscoe", "945", 'M', 
                1988,9,15,20,30);
        /**
         * Display Staff, faculty, and Part-time
         */
        for(int i = 0; i<emps.length;i++)
        {
            if(emps[i] instanceof Staff)
            {
                System.out.println("\n"+emps[i]);
            }//end of if statement
            if(emps[i] instanceof Faculty)
            {
                System.out.println("\n"+emps[i]);
            }//end of if statement

        }// end of for loop

        //b
        System.out.println("\nTotal montly Salary for all employees");
        for(int i = 0; i<emps.length; i++)
        { 
            sum = emps[i].monthlyEarning()+sum;
        }
        System.out.println("$"+sum);
        //c
        System.out.println("\nTotal monthly salary for all faculuty");
        for(int i = 0; i<emps.length;i++)
        {
            if(emps[i] instanceof Faculty)
            {
                total = emps[i].monthlyEarning()+total;
            }
        }
        System.out.println("$"+total);

        //d Duplicate a faculty object. test the duplication
        Faculty f1 = (Faculty)emps[4];
        Faculty f2 = (Faculty)f1.clone();
        Education dupl = new Education("Bachelor",
                "Airforce",2);
        f2.setEducation(dupl);
        System.out.println("\nD Duplicate a Faculty Object"
                +"\n"+f2.toString());

        //E Verify two staff objects are the same

        System.out.println("\nE.Verify two staff objects ");
        Staff s1 = (Staff)emps[6];
        Staff s2 = (Staff)s1.clone();
        Staff s3 = new Staff("Victor Tran", "456", 'M', 
        1993, 5, 20,60);
        if(s1.getbirthday()==s2.getbirthday())
        {
            System.out.print("\nThe two staff objects " +
                    " birthdays"+ " are the same "
                    +"therefore "+true+"\n");
        }
        if(s3.getbirthday()==s1.getbirthday())
        {
            System.out.print(true);
        }

        //F Sort employees by ascending employee ID
        System.out.println("\nSort employees by ID");
        Arrays.sort(emps);
        for(int i=0;i<emps.length;i++)
        {
            System.out.println("\n"+emps[i]);
        }

错误输出为

  

线程“main”中的异常java.lang.ClassCastException:无法将人员强制转换为java.lang.Comparable       at java.util.Arrays.mergeSort(Arrays.java:1144)       在java.util.Arrays.mergeSort(Arrays.java:1155)       在java.util.Arrays.sort(Arrays.java:1079)       在tester.main(tester.java:93)

我的员工课看起来像这样:

public class Staff extends Employee implements EmployeeInfo,Cloneable
{
    private double hourlyRate;

/**
 * Default Constructor
 */
public Staff()
{
    super();
    hourlyRate = 0.0;
}
/**
 * Argument Constructor
 * @param name
 * @param number
 * @param g
 * @param date
 * @param rate
 */
public Staff(String name, String number, char g, 
        int year, int month, int day,double rate)
{
    super(name,number,g,year,month,day);
    hourlyRate = rate; 
}
/**
 * Sets the hourly rate
 * @param rate
 */
public void setHourlyRate(double rate)
{
    hourlyRate = rate; 
}
/**
 * Get the hourly rate
 * @return hourlyRate
 */
public double getHourlyRate()
{
    return hourlyRate;
}
public double monthlyEarning()
{
    double salary = STAFF_MONTHLY_HOURS_WORKED* hourlyRate;
    return salary;
}

public String toString()
{
    return super.toString() +"\nFull Time"+"\nMonthly Salary: $"
                + monthlyEarning()+"\nHourly Rate: $"+hourlyRate;
}
public Object clone() throws CloneNotSupportedException
{
    Staff s = (Staff)super.clone();
    return s;
}
}// End of Staff Class

1 个答案:

答案 0 :(得分:0)

使您的对象(Staff等)实现Comparable接口并使用ID进行比较。如果需要,检查Comparable接口的使用情况,这非常简单。没有Arrays.sort将不知道如何对对象进行排序