问题描述
我想要有一个compareTo方法,它需要一个Real(一个用于处理任意大的和精确的实数的类([只要它现在的长度小于2 ^ 31]]和compareTo方法需要一个对象,但Java不会让我,我没有足够的经验知道为什么。
I'd like to have a compareTo method that takes a Real (a class for working with arbitrarily large and precise real numbers [well, as long as it's less than 2^31 in length at the moment]) and a compareTo method that takes an Object, but Java isn't letting me and I'm not experienced enough to know why.
我试图修改类来实现Comparable和我在下面看到这些错误消息。我不太明白错误消息的含义,但我知道这与我想让每个方法的所有不同方法签名具有灵活性的可怕方法有关,并且我可以修复它通过删除compareTo(Object other)方法,但我最好喜欢保留它。所以我真正要问的是:有没有办法让这些错误信息消失而不删除compareTo(Object other)方法,这些错误究竟意味着什么?
I just tried to modify the class to implement Comparable and I got these error messages below. I don't really understand what the error messages mean but I know it's got something to do with the horrible way I'm trying to give the class some flexibility with all the different method signatures for every single method I make, and I can fix it by removing the compareTo(Object other) method, but I would ideally like to keep it. So what I'm really asking is: Is there a way to make these error messages disappear without removing the compareTo(Object other) method and what exactly do these errors mean?
另外,我知道已经有一些像BigInteger这样的内置Java类以及类似于我正在尝试使用此类的东西,但是我为了使用Project Euler而获得乐趣/满意度()。
Also, I know there are already some built-in Java classes like BigInteger and things like that for what I'm trying to use this class for but I'm doing it for fun/satisfaction for use with Project Euler (https://projecteuler.net/).
Jake@Jake-PC /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方法:
These are the compareTo methods:
@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())); }
以及构造函数以防万一您需要它们:
and the constructors just in case you need them:
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()); }
推荐答案
有问题的方法是:
@Override public int compareTo(Real other) { ... } public int compareTo(Object other) { ... }
这些方法具有相同的,这意味着一旦编译器去掉通用类型信息,不再是在运行时区分它们的一种方式。
These methods have the same erasure, meaning that once the compiler strips out generic type information, there will no longer be a way to differentiate them at runtime.
您的选项是删除 compareTo(Object other)重载,或 Real 来实现 Comparable< Object> 。
Your options are to either remove the compareTo(Object other) overload, or for Real to implement Comparable<Object>.
由于它看起来像所有 compareTo 重载的实现,只是实例化一个新的 Real 并传递给 compareTo(Real),我建议删除它们并将该转换留给调用者:
Since it looks like the implementations of all your compareTo overloads just instantiate a new Real and pass it to compareTo(Real), I'd suggest removing them and leaving that conversion up to the caller:
Real real = ...; Object compared = ...; Real comparedAsReal = new Real(compared); int result = real.compareTo(comparedAsReal);
这篇关于实现Comparable,compareTo name冲突:“具有相同的擦除,但都不会覆盖另一个”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!