JAVA:覆盖接口方法

时间:2014-05-27 23:48:02

标签: java methods interface override

我有界面:

interface operations{  
    void sum();  
}   

我希望上课:

class matrix implements operations {  
    @override   
    void sum(matrix m) {  
    } 
}

class vector3D implements operations {  
    @override  
    void sum(vecor3D v) {  
    }
}

怎么做? 我试过这样的事情:

interface operations < T > {  
    <T> void sum(T t);  
}

class matrix implements operations<matrix>{
    @Override
    void sum(matrix m){};
    }
}

class vector3D implements operations<vector3D>{
    @Override
    void sum(vector3D v){};
}

但它不起作用。

1 个答案:

答案 0 :(得分:6)

请勿在界面类型中添加类型参数。您还应该指定所实现的接口的通用参数:

interface operations<T> {  
    void sum(T t);  
}



class matrix implements operations<matrix> {  
    @Override   
    public void sum(matrix m){  
    } 
}



class vector3D implements operations<vecor3D> {  
    @Override  
    public void sum(vecor3D v){  
    }
}