加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 具有不同签名的覆盖方法

发布时间:2020-12-15 00:35:03 所属栏目:Java 来源:网络整理
导读:我有一个超类使用该方法: protected E extends EnumE,T extends VO void processarRelatorioComEstado(Date dataInicial,Date dataFinal,E estado) throws RelatorioException { throw new UnsupportedOperationException("method not overridden");} 在其
我有一个超类使用该方法:
protected <E extends Enum<E>,T extends VO> void processarRelatorioComEstado(Date dataInicial,Date dataFinal,E estado) throws RelatorioException {

    throw new UnsupportedOperationException("method not overridden");
}

在其中一个子类中,我想要执行以下操作:

@Override
protected <E extends Enum<E>> DemonstrativoReceitaDespesasAnexo12Vo processarRelatorioComEstado(Date dataInicial,E estado) throws RelatorioException {
//do something
return DemonstrativoReceitaDespesasAnexo12Vo;
}

但这只是行不通.问题是我有一个超类的引用,我想调用这个方法,但只能在其中一个子类中调用.

解决方法

您无法在重写方法中更改类型参数的数量.至于你的情况,覆盖明显失败,返回类型.但即使返回类型相同,您的方法仍然不会覆盖等效,因为您在所谓的重写方法中具有较少的类型参数.

从JLS – Method Signature开始:

Two methods have the same signature if they have the same name and
argument types.

Two method or constructor declarations M and N have the same argument
types if all of the following conditions hold:

  • They have the same number of formal parameters (possibly zero)
  • They have the same number of type parameters (possibly zero)

因此,即使以下代码也会失败:

interface Demo {
    public <S,T> void show();
}

class DemoImpl implements Demo {
    @Override
    public <T> void show() { }  // Compiler error
}

由于类型参数较少,因此类中的方法show()不会与接口中的方法等效.

因此,您应该确保方法签名与JLS部分中指定的完全相同(相同名称,相同数量和类型的参数(包括类型参数),共变量返回类型).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读