推断类型不是有效的替代类型 [英] Inferred type is not a valid substitute for a Comparable generic type

查看:621
本文介绍了推断类型不是有效的替代类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑代码:

  public abstract class Item< T>实现可比较的< T> 
{
protected T item;

public int compareTo(T o)
{
return 0; //这不重要的时间是
}
}

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

public class Foo< T>
{
protected ArrayList< T>列表;
}

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



排序调用错误:


绑定不匹配:类型集合的通用方法排序(List< T>)不适用于参数(ArrayList& MyItem< T>>)。推断类型MyItem< T>不是有界参数的有效替换T延伸, ? super T>>



为什么会出错?



如果 MyItem< V> 实现 Comparable ,那么为什么它不是替代品?


$

解决方案

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


java: 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> java.lang.Comparable< ;? super MyItem< V>>


因此,主要问题是:
$ b < T> sort(java.util.List< T>))不适用?



答案是

,因为在集合中。< T> sort(java.util.List< ; T>)方法声明在参数 T 上有边界:< T extends Comparable < super T>>



换句话说, T 必须实现 Comparable 对它自我。例如 String 类实现这样的接口: ... implements ... Comparable< String>



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



Item< T>实现可比较的< T> 不同于 Item< T>实现可比较的< Item< T>>



因此,为了解决这个问题,你应该改变你的 class to this one:

  public abstract class Item< T>实现可比较的< Item< T> 
{
protected T item;

public int compareTo(Item< T> o)
{
return 0; //这不重要的时间是
}
}


Consider the code:

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


The sort call gives the error:

Bound mismatch: The generic method sort(List< T >) of type Collections is not applicable for the arguments (ArrayList< MyItem< T > >). The inferred type MyItem< T > is not a valid substitute for the bounded parameter < T extends Comparable< ? super T > >


Why is this wrong?

If MyItem<V> implements Comparable then why is it not a substitute?

Sorry if this has been asked, but I feel the question is somewhat specific.

解决方案

Actually more detailed explanation of this error gives your javac itself:

java: no suitable method found for sort(java.util.ArrayList<MyItem<V>>)

method java.util.Collections.<T>sort(java.util.List<T>,java.util.Comparator<? super T>) is not applicable (cannot instantiate from arguments because actual and formal argument lists differ in length)

method java.util.Collections.<T>sort(java.util.List<T>) is not applicable (inferred type does not conform to declared bound(s) inferred: MyItem<V> bound(s): java.lang.Comparable<? super MyItem<V>>)

So, the main question is:
why is method Collections.<T>sort(java.util.List<T>)) not applicable?

The answer is:
because in Collections.<T>sort(java.util.List<T>) method declaration there are bounds on parameter T: <T extends Comparable<? super T>>.

In another words, T must implement Comparable interface on it self. For example String class implements such interface: ...implements ... Comparable<String>.

In your case Item class doesn't implement such interface:

Item<T> implements Comparable<T> is not same thing as Item<T> implements Comparable<Item<T>>.

So, for solving this problem, your should change your Item class to this one:

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

这篇关于推断类型不是有效的替代类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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