JAVA GENERICS ERROR:具有相同的擦除,但不会覆盖另一个 [英] JAVA GENERICS ERROR: have the same erasure, yet neither overrides the other

查看:111
本文介绍了JAVA GENERICS ERROR:具有相同的擦除,但不会覆盖另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了好玩,我创建了一个排序框架,以更好地理解各种排序算法。而且,我试图使它足够通用,以便它可以对实现可扩展可比接口的接口的任何东西进行排序。然而,java编译器并不满意我。



这是我的界面:

  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>)具有相同的擦除,但都不会覆盖其他

感谢您的帮助! 试着写实现Sorter< C> 。如果没有type参数,则使用原始类型,这会禁用继承方法的泛型的某些方面。具体而言,对于原始超类型,您只能继承已擦除的方法签名。在你的情况下,而不是继承方法 sort(C [] comparables)你继承了一个方法 sort(Comparable [] comparables) $ b

通过指定 Sorter< C> 作为$ c>,您只能用相同的签名覆盖。你可以继承一个方法`sort(C [] comparables),你可以用相同的签名覆盖它。



这就是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:

public interface Sorter<C extends Comparable<C>>
{
    void sort(C[] comparables);

    void sort(C[] comparables, Comparator<C> comparator);
}

And, here's my abstract class that implements that interface:

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

And, here are the errors I'm getting:

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

Thanks in advance for your help!

解决方案

Try writing 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.

By specifying Sorter<C> as the supertype, you inherit a method `sort(C[] comparables), which you can override with the identical signature.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆