2013-06-14 4 views
14

Real(任意の大きさと正確な実数を扱うためのクラス[限り現時点では2^31以下です)]とObjectを受け取るcompareToメソッドがありますが、Javaは私を放置していません。理由を知るのに十分な経験はありません。Comparableを実装するcompareTo名前のクラッシュ: "同じ消去を持ちますが、どちらもどちらも上書きしません"

私は、Comparableを実装するためにクラスを修正しようとしましたが、私は以下のエラーメッセージを受け取りました。私はエラーメッセージの意味を理解していませんが、恐ろしいやり方とは何かがあることを私は知っています。私が作るすべてのメソッドごとに異なるメソッドシグネチャを持つ柔軟性をクラスに与えようとしています。それはcompareTo(Object other)メソッドを削除することによって行いますが、私は理想的にそれを保持したいと考えています。だから私が本当に求めているのは、compareTo(Object other)メソッドを削除せずにこれらのエラーメッセージが消えるようにする方法はありますか?これらのエラーはどういう意味ですか?

また、BigIntegerのような組み込みのJavaクラスがいくつかありますが、このクラスを使用しようとしているようなものがありますが、Project Eulerで使用するための楽しい/満足のためにhttps://projecteuler.net/)。

[email protected] /cygdrive/c/Users/Jake/Documents/Java/Mathematics 
$ javac Real.java 
Real.java:377: error: name clash: compareTo(Real) in Real overrides a method whose erasure is the same as another method, yet neither overrides the other 
    public int compareTo(Real other) 
      ^
    first method: compareTo(Object) in Real 
    second method: compareTo(T) in Comparable 
    where T is a type-variable: 
    T extends Object declared in interface Comparable 
Real.java:440: error: name clash: compareTo(Object) in Real and compareTo(T) in Comparable have the same erasure, yet neither overrides the other 
    public int compareTo(Object other) 
      ^
    where T is a type-variable: 
    T extends Object declared in interface Comparable 
2 errors 

これらは、のcompareToメソッドです:

@Override 
    public int compareTo(Real other) 
    { 
    // Logic. 
    } 
    public int compareTo(char givenValue) 
    { return compareTo(new Real(givenValue)); } 
    public int compareTo(char[] givenValue) 
    { return compareTo(new Real(givenValue)); } 
    public int compareTo(char[] givenValue, int offset, int count) 
    { return compareTo(new Real(givenValue, offset, count)); } 
    public int compareTo(double givenValue) 
    { return compareTo(new Real(givenValue)); } 
    public int compareTo(float givenValue) 
    { return compareTo(new Real(givenValue)); } 
    public int compareTo(int givenValue) 
    { return compareTo(new Real(givenValue)); } 
    public int compareTo(long givenValue) 
    { return compareTo(new Real(givenValue)); } 
    public int compareTo(Object other) 
    { return compareTo(new Real(other.toString())); } 

とコンストラクタあなたがそれらを必要とするだけの場合:

public Real(String givenValue) 
    { 
    // Logic. 
    } 
    public Real(char givenValue) 
    { this(String.valueOf(givenValue)); } 
    public Real(char[] givenValue) 
    { this(String.valueOf(givenValue)); } 
    public Real(char[] givenValue, int offset, int count) 
    { this(String.valueOf(givenValue, offset, count)); } 
    public Real(double givenValue) 
    { this(String.valueOf(givenValue)); } 
    public Real(float givenValue) 
    { this(String.valueOf(givenValue)); } 
    public Real(int givenValue) 
    { this(String.valueOf(givenValue)); } 
    public Real(long givenValue) 
    { this(String.valueOf(givenValue)); } 
    public Real(Object other) 
    { this(other.toString()); } 
+0

あなたは私たちに、クラス全体を表示することができますか? –

答えて

14

問題の方法は次のとおりです。

@Override 
public int compareTo(Real other) { ... } 

public int compareTo(Object other) { ... } 

これらのメソッドは同じです。erasureは、コンパイラがジェネリック型情報を取り除くと、実行時にそれらを区別する方法がなくなることを意味します。

compareTo(Object other)オーバーロードを削除するか、RealComparable<Object>を実装するかのいずれかを選択します。

それはちょうど新しいRealをインスタンス化し、compareTo(Real)に渡し、すべてのcompareToのオーバーロードの実装のように見えるので、私はそれらを削除し、呼び出し側までその変換を残すことをお勧めしたい:

Real real = ...; 
Object compared = ...; 

Real comparedAsReal = new Real(compared); 
int result = real.compareTo(comparedAsReal); 
1

これは、副作用Javaジェネリック型消去

あなたは、汎用インタフェースを実装匹敵するが、消去この独自の方法で、一度ジェネリック型はのcompareTo(Object)をになるだろうされている、したがってそれはあなた自身ののcompareTo(Object)をと衝突します。ここで

が再現する最小限のコードです:

class Real implements Comparable<Real> 
{ 
    public int compareTo(Object o) 
    { 
     return 0; 
    } 

    @Override 
    public int compareTo(Real o) 
    { 
     return 0; 
    }  
} 
3

あなたはObjectRealオブジェクトを比較することができるようにしたいので、あなただけのimplements Comparable<Object>implements Comparable<Real>を置き換えることができます。これは<T> the type of objects that this object may be compared toというComparable<T> javadocと一貫しています。

次に、あなただけにあなたの現在のコードを変更する必要があります。

// No more @Override 
public int compareToReal(Real other) 
{ 
    // Logic. 
} 
public int compareTo(char givenValue) 
{ return compareToReal(new Real(givenValue)); } 
public int compareTo(char[] givenValue) 
{ return compareToReal(new Real(givenValue)); } 
public int compareTo(char[] givenValue, int offset, int count) 
{ return compareToReal(new Real(givenValue, offset, count)); } 
public int compareTo(double givenValue) 
{ return compareToReal(new Real(givenValue)); } 
public int compareTo(float givenValue) 
{ return compareToReal(new Real(givenValue)); } 
public int compareTo(int givenValue) 
{ return compareToReal(new Real(givenValue)); } 
public int compareTo(long givenValue) 
{ return compareToReal(new Real(givenValue)); } 
@Override 
public int compareTo(Object other) 
{ return compareToReal(new Real(other.toString())); } 
関連する問題