0

テスター クラスで配列ソート関数を使用すると、エラーが発生します。

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]);
        }

エラーは次のように出力されます

スレッド「メイン」での例外 java.lang.ClassCastException: スタッフは java.util.Arrays.mergeSort(Arrays.java:1144) で java.lang.Comparable にキャストできません 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
4

1 に答える 1

0

オブジェクト (Staff など) に Comparable インターフェイスを実装させ、ID を使用して比較します。必要に応じて Comparable インターフェイスの使用を確認してください。これは非常に簡単です。それがなければ、Arrays.sort はオブジェクトの並べ替え方法を認識しません。

于 2013-02-15T17:58:02.587 に答える