JAVA:覆盖接口方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23900735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:55:57  来源:igfitidea点击:

JAVA: override interface method

javamethodsinterfaceoverriding

提问by user3308470

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.

但它不起作用。

采纳答案by fabian

Don't add a type parameters to the interface andthe 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){  
    }
}