为什么这个通用接口实现不能编译?

时间:2011-03-08 08:56:37

标签: java generics compiler-errors

为什么这个通用接口实现不能编译?

//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 个答案:

答案 0 :(得分:3)

 class Client<T> implements IClient<T> {

答案 1 :(得分:2)

哦,我发现有一个名字冲突: Client类型的compareTo(IClient)方法与IClient类型的compareTo(IClient)具有相同的擦除功能,但不会覆盖它。

IClient是原始类型。对泛型类型IClient的引用应该参数化

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