我做了一个"People“类,它有一个字符串名。现在,我想使用TreeSet比较两个对象。
public class People<T> implements Comparable<T> {
public TreeSet<People> treeSet;
public String name;
public People(String name)
{
treeSet = new TreeSet();
this.name = name;
}.
@Override
public int compareTo(T y) {
if(this.name.equals(y.name)) blablabla; //Here I get error
}错误:
Cannot find symbol
symbol: variable name;
location: variable y of type T
where T is a type variable
T extends Object declared in class OsobaSet有人知道如何解决这个问题吗?
发布于 2016-01-20 10:07:29
Comparable接口中的泛型类型表示要比较的对象类型。
对于您的示例,这是正确的用法:
public class People implements Comparable<People>在这种情况下,方法签名将是
@Override
public int compareTo(People y) {
if (this.name.equals(y.name)) { ...
}https://stackoverflow.com/questions/34896323
复制相似问题