gpt4 book ai didi

java - 实现 Comparable、compareTo 名称冲突 : "have the same erasure, yet neither overrides the other"

转载 作者:搜寻专家 更新时间:2023-10-30 19:41:57 24 4
gpt4 key购买 nike

我想要一个 compareTo 方法,它接受一个 Real(一个用于处理任意大且精确的实数的类 [好吧,只要它现在的长度小于 2^31])和一个 compareTo接受对象的方法,但 Java 不允许,而且我没有足够的经验知道原因。

我刚刚尝试修改类以实现 Comparable,但在下面收到了这些错误消息。我真的不明白错误消息是什么意思,但我知道这与我试图通过我创建的每个方法的所有不同方法签名为类提供一些灵 active 的可怕方式有关,我可以修复它通过删除 compareTo(Object other) 方法,但理想情况下我想保留它。所以我真正想问的是:有没有一种方法可以在不删除 compareTo(Object other) 方法的情况下使这些错误消息消失,这些错误到底意味着什么?

此外,我知道已经有一些内置的 Java 类,如 BigInteger 和类似的东西,用于我尝试使用此类的目的,但我这样做是为了乐趣/满足于与 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 方法:

  @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()); }

最佳答案

违规的方法是:

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

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

这些方法具有相同的erasure ,这意味着一旦编译器剥离泛型类型信息,将无法在运行时区分它们。

您的选择是删除 compareTo(Object other)过载,或为 Real实现Comparable<Object> .

因为它看起来像所有 compareTo 的实现重载只是实例化一个新的 Real并将其传递给 compareTo(Real) ,我建议删除它们并将转换留给调用者:

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

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

关于java - 实现 Comparable、compareTo 名称冲突 : "have the same erasure, yet neither overrides the other",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17116845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com