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()); }
あなたは私たちに、クラス全体を表示することができますか? –