java - 无法覆盖通用接口(interface)

标签 java generics interface overriding abstract-class

我正在尝试创建泛型类型方法和接口(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/

相关文章:

java - HSQLDB 内存数据库 JDBC 模板查询失败

java - 使用 RabbitMQ 将 python 字典发送到 java

c# - 如何实现 IComparable 接口(interface)?

typescript - 带有构造签名的接口(interface)如何工作?

JAVA : How to remove duplicates from list using stream

Java构造函数引用赋值 VS 新建对象赋值

java - 可调用和泛型的集合

c# - 泛型类约束,其中 <T> 是约束泛型类的类型

Swift:类型不符合泛型协议(protocol)

reflection - 我们如何使用空接口(interface)指向的实际类型在 Go 中创建该类型的新实例?