我有一个超类使用该方法:
- 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;
- }
解决方法
您无法在重写方法中更改类型参数的数量.至于你的情况,覆盖明显失败,返回类型.但即使返回类型相同,您的方法仍然不会覆盖等效,因为您在所谓的重写方法中具有较少的类型参数.
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部分中指定的完全相同(相同名称,相同数量和类型的参数(包括类型参数),共变量返回类型).