2013-02-15 2 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 캐스트 할 수없는 (Arrays.java:1144) at java.util.Arrays.mergeSort (Arrays.java:1155) at java.util.Arrays.sort (Arrays.java:1079) at 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 

답변

0

이 개체를 확인 (직원 등) Comparable 인터페이스를 구현하고 비교를 위해 ID를 사용합니다. 필요한 경우 Comparable 인터페이스의 사용을 확인하십시오, 꽤 간단합니다 .Arrays.sort는 객체를 정렬하는 방법을 알지 못합니다.

+0

저는 abstract 클래스 Called employee를 사용하고 있으며이 클래스를 다른 객체의 상속으로 사용하고 있습니다. 추상 클래스에서 Comparable을 먼저 구현해야합니까? 현재 내 스태프는 "Public Staff Staff extends EmployeeInplete, Cloneable im extends EmployeeInfo, Cloneable"과 같이 보입니다. 이후에 ""라인을 추가해야합니까? –

+0

그렇지만 더 중요한 것은 compareTo 메소드를 구현해야한다는 것입니다. 예를 들어 이것을 확인하십시오. http://javarevisited.blogspot.com/2011/06/comparator-and-comparable-in-java.html 추상 클래스 또는 비교를위한 모든 속성을 가져야합니다. ID가 필요하기 때문에 기본 클래스에서 처리 할 수 ​​있습니다. – Rohit

+0

고마워. 지금 일하고있어. –

관련 문제