java 推断类型不是 Comparable 泛型类型的有效替代品
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17739720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inferred type is not a valid substitute for a Comparable generic type
提问by terrorcell
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 > >
绑定不匹配:Collections 类型的泛型方法 sort(List< T >) 不适用于参数 (ArrayList< MyItem< T > >)。推断类型 MyItem< T > 不是有界参数 < T extends Comparable< 的有效替代品?超级T>>
Why is this wrong?
为什么这是错误的?
If MyItem<V>
implements Comparable
then why is it not a substitute?
如果MyItem<V>
实现,Comparable
那么为什么它不是替代品?
Sorry if this has been asked, but I feel the question is somewhat specific.
对不起,如果有人问过这个问题,但我觉得这个问题有点具体。
采纳答案by Andremoniy
Actually more detailed explanation of this error gives your javac
itself:
实际上,对这个错误的更详细的解释给了你javac
自己:
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>>
)
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>>
)
So, the main question is:
why is method Collections.<T>sort(java.util.List<T>)
) not applicable?
所以,主要问题是:
为什么 method Collections.<T>sort(java.util.List<T>)
) 不适用?
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>>
.
答案是:
因为在Collections.<T>sort(java.util.List<T>)
方法声明中参数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>
.
换句话说,T
必须自己实现Comparable
接口。例如String
类实现了这样的接口:...implements ... Comparable<String>
.
In your case Item
class doesn't implement such interface:
在您的情况下,Item
类没有实现这样的接口:
Item<T> implements Comparable<T>
is not same thing as Item<T> implements Comparable<Item<T>>
.
Item<T> implements Comparable<T>
与 不同Item<T> implements Comparable<Item<T>>
。
So, for solving this problem, your should change your Item
class to this one:
所以,为了解决这个问题,你应该把你的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
}
}
回答by Bernhard Barker
For objects of type X
to be comparable with each other, class X
has to implement exactly Comparable<X>
.
要使类型的对象X
相互比较,类X
必须完全实现Comparable<X>
。
This is not what your code is doing, you've got a class Item<T>
and you are implementing Comparable<T>
instead of Comparable<Item<T>>
. This means that Item<T>
can be compared with T
, but not with Item<T>
, which is required.
这不是你的代码在做什么,你有一个类Item<T>
并且你正在实现Comparable<T>
而不是Comparable<Item<T>>
. 这意味着Item<T>
可以与 进行比较T
,但不能与进行比较Item<T>
,这是必需的。
Change your Item<T>
class to:
将您的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
}
}
回答by Sstx
Just change the class like follow:
只需更改类,如下所示:
public class MyItem<T> extends Item<String> implement Comparable<MyItem<T>>
{
T object;
}
Or
或者
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
}
}
}
The error tips has shown us.Hope it helpful.
错误提示已向我们展示。希望对您有所帮助。
回答by Seelenvirtuose
You do not need to have the class MyItem
generified just to see the effect. The following class is enough to see what happens:
您不需要MyItem
为了查看效果而对类进行泛化。下面的类足以看看会发生什么:
public class MyItem extends Item<String> {}
Now you have the following call:
现在您有以下调用:
Collections.sort(list);
As morgano stated correctly, the sort method will take a collection that is parameterized with a type T that must be comparable to T. Your MyItem
class is extending Item<String>
, which results in MyItem
being comparable to String
s.
正如摩根诺正确指出的那样,排序方法将采用一个参数化类型 T 的集合,该类型必须与 T 相比较。您的MyItem
类正在扩展Item<String>
,这导致MyItem
与String
s相比较。
With a little switch in which class implements the Comparable
interface, you will get the expected result:
稍微切换一下哪个类实现了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
}
}
And now the call to Collections.sort(list)
will work.
现在调用Collections.sort(list)
将起作用。