我正在尝试创建泛型类型方法和接口(interface),然后在子类中实现它们,依此类推。但我无法弄清楚为什么我的子类会抛出一个错误,表明我没有从接口(interface)中覆盖抽象方法。代码如下:
package stdmathops1;
public interface StdMathOps1<T>{
public <T> T div(T f, T s);
}
//this is all very simple and not the whole of it, at this point I'm just
//trying to figure out why it won't override the div method.
class Fraction implements StdMathOps1{
public static void main(String[] args){
}
public <T extends StdMathOps1> T div(T f, T s){
return f;
}
}
无论我在哪里查看重写和使用接口(interface),它似乎都是正确的,但它仍然会抛出
Fraction is not abstract and does not override abstract method div(Object, Object)
in StdMathOps1
任何帮助将不胜感激
最佳答案
您通常无法像更改重写方法那样更改参数的界限。对于编译器来说,
public <T> T div(T f, T s);
和
public <T extends StdMathOps1> T div(T f, T s);
是两种不同的方法。
有关详细信息,请参阅Cannot override generic interface
关于java - 无法覆盖通用接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23438813/