Chciałbym mieć metodę compareTo, która pobiera Real (klasa do pracy z dowolnie dużymi i precyzyjnymi liczbami rzeczywistymi [dobrze, o ile w tym momencie jest mniej niż 2^31) i metoda compareTo, która przyjmuje obiekt, ale Java nie pozwala mi, a ja nie jestem wystarczająco doświadczony, aby wiedzieć, dlaczego.Wdrażanie Porównywalne, porównawcze zderzenie nazw: "mają to samo wymazanie, ale żaden nie zastępuje innego"
Po prostu próbowałem zmodyfikować klasę, aby zaimplementować Porównywalne i dostałem te komunikaty o błędach poniżej. Naprawdę nie rozumiem, co oznaczają komunikaty o błędach, ale wiem, że ma to coś wspólnego z okropnym sposobem, w jaki próbuję nadać klasie pewną elastyczność z różnymi sygnaturami metod dla każdej tworzonej przeze mnie metody, i mogę naprawić poprzez usunięcie metody compareTo (Object other), ale najlepiej byłoby ją zachować. Tak naprawdę pytam: Czy istnieje sposób, aby te komunikaty o błędach zniknęły bez usuwania metody compareTo (Object other) i co dokładnie oznaczają te błędy?
Ponadto wiem, że istnieją już wbudowane klasy Java, takie jak BigInteger i podobne rzeczy, dla których próbuję korzystać z tej klasy, ale robię to dla zabawy/satysfakcji do użycia z 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
Są to metody 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())); }
i konstruktorzy tylko w przypadku trzeba je:
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()); }
Może pokażesz nam cała klasa? –