无法重写通用接口 [英] Cannot override generic interface

查看:123
本文介绍了无法重写通用接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建和接口泛型类型的方法,然后在子类中实现它们等等。但我无法弄清楚为什么我的子类抛出一个错误,说我没有重写从我的界面抽象方法。代码如下:

  package stdmathops1; 
public interface StdMathOps1< T> {
public< T> T div(T f,T s);
}
//这是非常简单的,而不是整个它,在这一点上,我只是
//试图找出为什么它不会覆盖div方法。
class Fraction implements StdMathOps1 {
public static void main(String [] args){
}
public< T extends StdMathOps1> T div(T f,T s){
return f;


$ / code>

无论我看到覆盖和使用接口, ,但它仍然会抛出

 分数不是抽象的,也不会重写抽象方法div(Object,Object)
in StdMathOps1

任何帮助都将不胜感激

解决方案

您通常不能像重写的方法那样更改参数的边界。到编译器,

  public< T> T div(T f,T s); 

  public< T extends StdMathOps1> T div(T f,T s); 

是两种不同的方法。

更多信息,请参阅无法覆盖通用界面


I'm trying to create and interface of generic type methods, then implement them in a subclass and so on. But I cannot figure out why my subclass throws an error that says I have not overridden the abstract method from my interface. Code is as follows:

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

Wherever I look about overriding and using interfaces it seems right, but it still throws

Fraction is not abstract and does not override abstract method div(Object, Object)
in StdMathOps1

Any help would be greatly appreciated

解决方案

You generally can't change bounds on parameters like you did on overridden methods. To the compiler,

public <T> T div(T f, T s);

and

public <T extends StdMathOps1> T div(T f, T s);

are two different methods.

For more information, see Cannot override generic interface

这篇关于无法重写通用接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆