Java 具有不同签名的覆盖方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18773840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Override method with different signature
提问by danillosl
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.
但这根本行不通。问题是我有一个超类的引用,我想调用这个方法,但只在一个子类中。
采纳答案by Rohit Jain
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:
- They have the same number of formal parameters (possibly zero)
- They have the same number of type parameters(possibly zero)
如果两个方法具有相同的名称和参数类型,则它们具有相同的签名。
如果以下所有条件都成立,则两个方法或构造函数声明 M 和 N 具有相同的参数类型:
- 它们具有相同数量的形式参数(可能为零)
- 它们具有相同数量的类型参数(可能为零)
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 部分中指定的完全相同(相同的名称、相同数量和类型的参数(包括类型参数)、协变返回类型)。
回答by Prabhaker A
According to java overridding
根据java覆盖
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.
在这里,您的方法返回类型不同,因此不会被覆盖。
回答by danillosl
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.
谢谢你们。