简体   繁体   English

JAVA:覆盖接口方法

[英]JAVA: override interface method

I have interface: 我有界面:

interface operations{  
    void sum();  
}   

and I want to have classes: 我想要上课:

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

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

How to do this? 这该怎么做? I tried something like this: 我试过这样的事情:

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){};
}

but it doesn't work. 但它不起作用。

Don't add a type parameters to the interface and the type. 不要在接口类型中添加类型参数。 Also you should specify the generic parameters of the interface you implement: 您还应该指定实现的接口的通用参数:

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){  
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM