JAVA GENERICS ERROR:具有相同的擦除,但不会覆盖另一个 [英] JAVA GENERICS ERROR: have the same erasure, yet neither overrides the other
问题描述
这是我的界面:
public interface Sorter< C extends Comparable< C>>
{
void sort(C [] comparables);
void sort(C [] comparables,Comparator< C> comparator);
}
这是我的抽象类,实现了这个接口:
public abstract class AbstractSort< C extends Comparable< C>>执行Sorter
{
protected abstract void doSort(C [] comparables,Comparator< C> comparator);
$ b $ public final void sort(C [] comparables)
{
sort(comparables,new Comparator< C>()
{
public int compare (C左,C右)
{
return left.compareTo(right);
}
});
final public void sort(C [] comparables,Comparator< C> comparator)
{
doSort(comparables,comparator);
}
}
这里是我得到的错误:
java:名称冲突:排序中的AbstractSort和sort(java.lang.Comparable [])中的sort(C [])具有相同的擦除,但都不会覆盖AbstractSort中的其他
错误:(25,23)java:name clash:sort(C [],java.util.Comparator< C>)和sort Sorter中的java.lang.Comparable [],java.util.Comparator< java.lang.Comparable>)具有相同的擦除,但都不会覆盖其他
感谢您的帮助! 试着写 通过指定 这就是Java Language Specification推荐的原因原始类型只能用于与非通用遗留代码进行交互,并且要求编译器在使用原始类型时发出警告。 For fun I'm creating a sorting framework to better understand the various sorting algorithms. And, I'm trying to make it generic enough so that it can sort anything that implements an interface that extends the comparable interface. However, the java compiler isn't happy with me. Here's my interface: And, here's my abstract class that implements that interface: And, here are the errors I'm getting: Thanks in advance for your help! Try writing By specifying Pitfalls such as this is why the Java Language Specification recommends that raw types should only be used for interfacing with non-generic legacy code, and requires compilers to emit a warning when raw types are used. 这篇关于JAVA GENERICS ERROR:具有相同的擦除,但不会覆盖另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!实现Sorter< C>
。如果没有type参数,则使用原始类型,这会禁用继承方法的泛型的某些方面。具体而言,对于原始超类型,您只能继承已擦除的方法签名。在你的情况下,而不是继承方法 sort(C [] comparables)
你继承了一个方法 sort(Comparable [] comparables)$ c
$ b Sorter< C>
作为$ c>,您只能用相同的签名覆盖。你可以继承一个方法`sort(C [] comparables),你可以用相同的签名覆盖它。
public interface Sorter<C extends Comparable<C>>
{
void sort(C[] comparables);
void sort(C[] comparables, Comparator<C> comparator);
}
public abstract class AbstractSort<C extends Comparable<C>> implements Sorter
{
protected abstract void doSort(C[] comparables, Comparator<C> comparator);
final public void sort(C[] comparables)
{
sort(comparables, new Comparator<C>()
{
public int compare(C left, C right)
{
return left.compareTo(right);
}
});
}
final public void sort(C[] comparables, Comparator<C> comparator)
{
doSort(comparables, comparator);
}
}
java: name clash: sort(C[]) in AbstractSort and sort(java.lang.Comparable[]) in Sorter have the same erasure, yet neither overrides the other
Error:(25, 23) java: name clash: sort(C[],java.util.Comparator<C>) in AbstractSort and sort(java.lang.Comparable[],java.util.Comparator<java.lang.Comparable>) in Sorter have the same erasure, yet neither overrides the other
implements Sorter<C>
. Without the type parameter, you use raw types, which disables certain aspects of generics for inherited methods. Specifically, with a raw super type, you only inherit erased method signatures. In your case, rather than inheriting the method sort(C[] comparables)
your inherit a method sort(Comparable[] comparables)
, which you can only override with an identical signature.Sorter<C>
as the supertype, you inherit a method `sort(C[] comparables), which you can override with the identical signature.