2013-02-15 27 views
0

錯誤的工作人員不得強制轉換爲java.lang.Comparable的

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被強制轉換爲java.lang.Comparable的 ( (tester.java:93)處的java.util.Arrays.sort(Arrays.java:1079) 處的java.util.Arrays.mergeSort(Arrays.java:1155) (在Arrays.java:1144)

我的工作人員類看起來是這樣的:

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 

回答

0

使對象(職員等)實現可比界面,並使用ID進行比較。檢查如果需要的話可比接口的使用,這是很simple.Without認爲Arrays.sort怎麼會不知道你的對象進行排序

+0

我使用一個名爲employee一個抽象類,使用類作爲我的其他對象繼承我是否必須首先在抽象類中實現Comparable?目前我的員工看起來像這樣「public class Staff extends Employee implements EmployeeInfo,Cloneable」,我只需要添加行Comparable 之後? – 2013-02-15 18:23:35

+0

是的,但更重要的是,你需要實現compareTo方法對於例如檢查此http://javarevisited.blogspot.com/2011/06/comparator-and-comparable-in-java.html實現它的抽象類或任何你具有所有的比較屬性。在你的情況下,因爲你只需要這個ID你可以在基類中做到這一點。 – Rohit 2013-02-15 18:25:52

+0

謝謝我現在工作 – 2013-02-15 18:46:38

相關問題