2011-08-04 10 views
0

でエラーをコンパイルします。Javaは、私は、このエラーに実行しているソート方法

ここではコードですが、HockeyPlayerクラス、Professor、Parent、GasStationのcompareToメソッドもオーバーライドしています。おかげさまで P

import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.Collections; 
/** 
* Class Employees. 
*/ 
public class Employees 
{ 
    private ArrayList<Employee> employeeList; 

    /** 
    * Constructor for objects of class Employees 
    */ 
    public Employees() 
    { 
     employeeList = new ArrayList<Employee>(); 
     //Creating 5 each types of Employees 
     employeeList.add(new HockeyPlayer("Wayne Gretzky", 894)); 
     employeeList.add(new HockeyPlayer("Who Ever", 0)); 
     employeeList.add(new HockeyPlayer("Brent Gretzky", 1)); 
     employeeList.add(new HockeyPlayer("Pavel Bure", 437)); 
     employeeList.add(new HockeyPlayer("Jason Harrison", 0)); 

     employeeList.add(new Professor("Albert Einstein", "Physics")); 
     employeeList.add(new Professor("Jason Harrison", "Computer Systems")); 
     employeeList.add(new Professor("Richard Feynman", "Physics")); 
     employeeList.add(new Professor("BCIT Instructor", "Computer Systems")); 
     employeeList.add(new Professor("Kurt Godel", "Logic")); 

     employeeList.add(new Parent("Tiger Woods", 1)); 
     employeeList.add(new Parent("Super Mom", 168)); 
     employeeList.add(new Parent("Lazy Larry", 20)); 
     employeeList.add(new Parent("Ex Hausted", 168)); 
     employeeList.add(new Parent("Super Dad", 167)); 

     employeeList.add(new GasStation("Joe Smith", 10)); 
     employeeList.add(new GasStation("Tony Baloney", 100)); 
     employeeList.add(new GasStation("Benjamin Franklin", 100)); 
     employeeList.add(new GasStation("Mary Fairy", 101)); 
     employeeList.add(new GasStation("Bee See", 1)); 
    } 

    /** 
    * Display the list of employee 
    */ 
    public void displayEmployees() 
    { 
     Iterator <Employee> it = employeeList.iterator(); 
     while(it.hasNext()) { 
      Employee e = it.next(); 
      System.out.println(e); 

     } 
    } 
    /** 
    * Display the list of employee sorted 
    */ 
    public void displaySortedEmployees() 
    { 
     **Collections.sort(employeeList);** 
     Iterator <Employee> it = employeeList.iterator(); 
     while(it.hasNext()) { 
      Employee e = it.next(); 
      System.out.println(e); 

     } 
    } 
} 

私が追加:Employeeクラスに匹敵する実装し、それが今でコンパイルが、私は違うsublclasses比較する必要がある:HockeyPlayer、親とのは.... メソッドを呼び出すとき、これは新しいエラーメッセージです: java.lang.ClassCastExceptionが、教授はここHockeyPlayer

にキャストすることはできませんが、サブクラスのいずれかです。

/** 
* Class HockeyPlayer. 
*/ 
public class HockeyPlayer extends Employee implements Employable, Comparable<Employee> 
{ 
    private  int  numberOfGoals; 
    private  double overTimePayRate ; 

    /** 
    * Constructor for objects of class Hockeyplayer 
    */ 
    public HockeyPlayer(String name, int numberOfGoals) 
    { 
     super(name); 
     overTimePayRate = 0.0; 
     this.numberOfGoals = numberOfGoals; 
    } 

    /** 
    * @return  overTimePayRate 
    */ 
    @Override 
    public double getOverTimePayRate() 
    { 
     return overTimePayRate; 
    } 
    @Override 
    public String getDressCode() 
    { 
     return "jersey"; 
    } 
    @Override 
    public boolean isPaidSalary() 
    { 
     return true; 
    } 
    @Override 
    public boolean postSecondaryEducationRequired() 
    { 
     return false; 
    } 
    @Override 
    public String getWorkVerb() 
    { 
     return "play"; 
    } 
    @Override 
    public boolean equals(Object that) 
    { 
     if(this == that){ 
      return true; 
     } 
     if(!(this instanceof HockeyPlayer)) { 
      return false; 
     } 
     HockeyPlayer h = (HockeyPlayer) that; 
     if(this.numberOfGoals == h.numberOfGoals) { 
      return true; 
     } 
     return false; 
    } 
    @Override 
    public int compareTo(Employee that) 
    { 
     if(this == that) { 
      return 0; 
     } 

     HockeyPlayer h = (HockeyPlayer) that; 

     if(this.numberOfGoals > h.numberOfGoals) { 
      return +1; 
     } 
     else { 
      return -1; 
     } 
    } 
} 

答えて

7

マイ推測は、あなたの時間ですEmployeeComparable<Employee>を実装していると宣言されていないので、public static <T extends Comparable<? super T>> void sort(List<T> list)は適用されません...しかし、あなたのEmployeeクラスに挑戦していないと言うのは難しいです。

class Employee {} 

が、私が使用したときに、それがコンパイルされます:私はあなたのコードをしようとすると、確かに私が取得エラーメッセージです

class Employee implements Comparable<Employee> { 

    // Obviously incorrect implementation, only used for demonstrating 
    // that the code will now compile. 
    public int compareTo(Employee other) { 
     return 0; 
    } 
} 

様々な他のクラスを想定しEmployeeのサブクラスである、あなたはHockeyPlayerParentをどのように比較するか(具体的にはどうすればよいか)を考える必要があります。 の従業員はのいずれかの他の従業員に匹敵する必要があることを忘れないでください。私の経験では、継承と比較(平等かソートかに関わらず)はめったにうまくいきません。

という短いプログラムを投稿することができれば、それは本当に役立ちます。

+0

Comparable を実装する代わりに、2番目のパラメータとしてComparator を使用するオーバーロードソート方法があります。あなたが選択するのは、従業員が自然にその種の他の人に匹敵するかどうか、またはあなたが投稿した方法の目的のためだけであるかどうかによって異なります。 – Mac

+0

@Mac:良い点。コンパレータを別々に提供する柔軟性が好きです。私は、Javaのさまざまなハッシュ/平等ベースの型が、.NETと同様に何か類似していることを望みます。 –

+0

ありがとうございました。私は自分のポストを更新しました.Jonは正しかったですし、今問題にぶつかっています。ClassCastException – bouchepat

1

Employeeクラスは、すべての子クラスにcompareTo(Employee)メソッドを実装するだけでなく、Comparable<Employee>インターフェイスを実装する必要があります。

0

ypuは、@Overrideアノテーションを使用しているので、Employeeがcomparableを実装していると宣言していると思います。また、私はあなたが実行時エラーではなく、コンパイル時のエラーを得るだろうこれを行っていない場合。私はあなたの質問のタイトルがコンパイルエラーと言っているので、ここで少し混乱していますが、本文には例外があると言われています。あなたの従業員クラスがcomparableを実装していても、あなたのサブクラスがcompareTo()の実装をオーバーライドする場合は、注意しないと問題が発生する可能性があります。ソートメソッドがhockeyPlayer以外のパラメータでhockeyPlayersのcompareTo()メソッドを呼び出すと、発生しているキャスト例外が発生します。私はあなたが@Overrideアノテーションを削除し、 "public int compareTo(HockeyPlayer that)"にシグネチャを変更すると、それが継承されたcompareTo()を使用するために動作し、non-hockeyPlayerの従業員と比較すると、 hockeyPlayersと比較して。 hockeyPlayerがEmployeeであっても、複数のオーバーロードされたメソッドが適用可能であれば、Javaはオーバーロードされたメソッドの中で最も特化したメソッドを使用します。つまり、オーバーライドではなく、ここでオーバーロードしたいと思います。また

、あなたが尋ねたが、私は尋ねる、それは価値があると思っていない何を:

if(this == that){ 
      return true; 
     } 

あなたが本当にここ==使用することを意味しますか?

関連する問題