2011-03-08 58 views
0

为什么这个通用的接口实现不兼容?为什么这个通用的接口实现不兼容?

//The type Client<T> must implement the inherited abstract method IClient.compareTo(IClient) 
class Client<T> implements IClient { 

    //The method compareTo(IClient<T>) of type Client<T> must override or implement a supertype method 
    //The Eclipse quick fix creates exactly the same supertype method which is defined in the interface. 
    @Override 
    public int compareTo(IClient<T> o) { 
     return this.getClass().getName().compareTo(o.getClass().getName()); 
    } 
} 

interface IClient<T> extends Comparable<IClient<T>> { 

    @Override 
    int compareTo(IClient<T> o); 

} 

回答

2

哦,我发现有一个名称冲突: 类型的客户端的方法的compareTo(IClient)具有相同的擦除为的compareTo(IClient)型在iClient但不覆盖它。

IClient是一种原始类型。参考通用类型IClient应参数化

class Client<T> implements IClient<T>将修复它。

+0

这就是@ysdx所说的。 – 2011-03-08 09:02:24

+0

@精英绅士:oliholz在ysdx之前发布了他的回答。 – 2011-03-08 09:11:03

+0

@Joachim Sauer:对不起,从来没有见过时间... – 2011-03-08 09:19:45

3
class Client<T> implements IClient<T> { 
+1

是的,发现它,它是如此简单。有时这有助于写下问题。 – oliholz 2011-03-08 09:03:28

相关问题