简体   繁体   English

具有不同签名的覆盖方法

[英]Override method with different signature

I have a superclass with the method: 我有一个超类使用该方法:

protected <E extends Enum<E>,T extends VO> void processarRelatorioComEstado(Date dataInicial, Date dataFinal, E estado) throws RelatorioException {

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

and in one of its subclasses I want to do the following: 在其中一个子类中,我想要执行以下操作:

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

but this just doesn't work. 但这只是行不通。 The problem is that I have a reference to a superclass, and I want to call this method, but only in one of the subclasses. 问题是我有一个超类的引用,我想调用这个方法,但只在一个子类中。

According to java overridding 根据java overridding

The overriding method has the same name, number and type of parameters, and return type as the method it overrides. 重写方法具有相同的名称,参数的数量和类型,并返回类型作为它重写的方法。 An overriding method can also return a subtype of the type returned by the overridden method. 重写方法还可以返回由重写方法返回的类型的子类型。 This is called a covariant return type. 这称为协变返回类型。

Here your method return type is different so it is not overridding. 这里你的方法返回类型是不同的,所以它不会被覆盖。

You can't change the number of type parameters in the overridden method. 您无法更改重写方法中的类型参数数。 As for your case, override clearly fails with the return type. 至于你的情况,覆盖明显失败,返回类型。 But even if the return types were same, your method still wouldn't be override equivalent, as you have fewer type parameters in the supposed-to-be overridden method. 但即使返回类型相同,您的方法仍然不会覆盖等效,因为您在所谓的重写方法中具有较少的类型参数。

From JLS - Method Signature : 来自JLS - 方法签名

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: 如果满足以下所有条件,则两个方法或构造函数声明M和N具有相同的参数类型:

  • They have the same number of formal parameters (possibly zero) 它们具有相同数量的形式参数(可能为零)
  • They have the same number of type parameters (possibly zero) 它们具有相同数量的类型参数 (可能为零)

So, even the following code would fail: 因此,即使以下代码也会失败:

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

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

As the method show() in class is not override equivalent with the method in interface, due to fewer type parameters. 由于类型参数较少,因此类中的方法show()不会与接口中的方法等效。

So, you should make sure that the method signature is exactly the same, as specified in that JLS section (Same name, same number and type of parameters (including type parameters), co-variant return type). 因此,您应该确保方法签名与JLS部分中指定的完全相同(相同名称,相同数量和类型的参数(包括类型参数),共变量返回类型)。

Reading the comments above, I understand that the approach wouldn't work, so I make some changes in the code and work like a charm, follows the code: 阅读上面的评论,我理解这种方法不起作用,所以我在代码中进行了一些更改,并像魅力一样工作,遵循以下代码:

superclass: 超类:

protected VO processarRelatorioComEstado(Date dataInicial, Date dataFinal, Enum<?> estado) throws RelatorioException {

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

and the subclass: 和子类:

public VO processarRelatorioComEstado(Date dataInicial, Date dataFinal, Enum<?> estado) throws RelatorioException {
//do something
return VOsubtype;

}

thanks guys. 多谢你们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM