繁体   English   中英

推断类型不是Comparable泛型类型的有效替代

[英]Inferred type is not a valid substitute for a Comparable generic type

考虑一下代码:

public abstract class Item<T> implements Comparable<T>
{
    protected T item;

    public int compareTo(T o)
    {
        return 0; // this doesn't matter for the time being
    }
}

public class MyItem<T> extends Item<String>
{
    T object;
}

public class Foo<T>
{
    protected ArrayList<T> list;
}

public class Bar<V> extends Foo<MyItem<V>>
{
    public void sort()
    {
        Collections.sort(list);
    }
}


排序调用给出错误:

绑定不匹配:类型集合的泛型方法sort(List <T>)不适用于参数(ArrayList <MyItem <T >>)。 推断类型MyItem <T>不是有界参数的有效替代<T extends Comparable <? 超级T >>


为什么这是错的?

如果MyItem<V>实现Comparable为什么它不能替代?

对不起,如果有人询问,但我觉得这个问题有点具体。

实际上这个错误的更详细的解释给你的javac本身:

java:没有为sort(java.util.ArrayList<MyItem<V>>找到合适的方法sort(java.util.ArrayList<MyItem<V>>

方法java.util.Collections.<T>sort(java.util.List<T>,java.util.Comparator<? super T>)不适用(无法从参数实例化,因为实际和形式参数列表的长度不同)

方法java.util.Collections.<T>sort(java.util.List<T>)不适用(推断类型不符合推断的声明边界: MyItem<V> bound(s): java.lang.Comparable<? super MyItem<V>>

所以,主要的问题是:
为什么方法Collections.<T>sort(java.util.List<T>) )不适用?

答案是
因为在Collections.<T>sort(java.util.List<T>)方法声明中有参数T界限: <T extends Comparable<? super T>> <T extends Comparable<? super T>>

换句话说, T必须在其上实现Comparable接口。 例如, String类实现了这样的接口: ...implements ... Comparable<String>

在您的情况下, Item类不实现这样的接口:

Item<T> implements Comparable<T>Item<T> implements Comparable<Item<T>>

因此,要解决此问题,您应该将Item类更改为此类:

public abstract class Item<T> implements Comparable<Item<T>>
{
    protected T item;

    public int compareTo(Item<T> o)
    {
        return 0; // this doesn't matter for the time being
    }
}

对于类型X对象彼此可比较,类X必须实现完全Comparable<X>

这不是你的代码正在做的事情,你有一个类Item<T> ,你正在实现Comparable<T>而不是Comparable<Item<T>> 这意味着Item<T>可以与T进行比较,但不能与Item<T> ,这是必需的。

Item<T>类更改为:

public abstract class Item<T> implements Comparable<Item<T>>
{
    protected T item;

    @Override
    public int compareTo(Item<T> o)
    {
        return 0; // this doesn't matter for the time being
    }
}

只需更改类如下:

     public class MyItem<T> extends Item<String> implement Comparable<MyItem<T>>
     {
         T object;
     }

要么

       public abstract class Item<T> implements Comparable<MyItem<T>>
       {
           protected T item;

           public int compareTo(MyItem<T> o)
           {
              return 0; // this doesn't matter for the time being
       }

}

错误提示向我们展示了。希望它有用。

您不需要将MyItem类放大,只是为了查看效果。 以下课程足以看出会发生什么:

public class MyItem extends Item<String> {}

现在您有以下电话:

Collections.sort(list);

正如morgano所说,sort方法将采用一个类型为T的集合,该类型必须与T相当。您的MyItem类正在扩展Item<String> ,这导致MyItemString s相当。

使用一个实现Comparable接口的小开关,您将获得预期的结果:

public abstract class Item<T> {
    protected T item;
}

public class MyItem extends Item<String> implements Comparable<MyItem> {
    @Override
    public int compareTo(MyItem o) {
        return item.compareTo(o.item); // just an example
    }
}

现在,对Collections.sort(list)的调用将起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM