什么是“覆盖等价”它与@Override有什么关系? [英] What is "override-equivalence" and how is it related to @Override?
问题描述
阅读 Javadoc 获取 @Override
注释,我遇到了以下规则:
Reading the Javadoc for the @Override
annotation, I came across the following rule:
如果使用此$ b $注释方法b注释类型编译器需要生成错误消息
,除非至少满足下列条件之一:
If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold:
- 该方法可以覆盖或实现在超类型中声明的方法。
- 该方法的签名覆盖等效于在Object中声明的任何公共方法
的签名。
- The method does override or implement a method declared in a supertype.
- The method has a signature that is override-equivalent to that of any public method declared in Object.
我很清楚第一点,但我不确定第二点一。
I'm clear on the first point, but I'm unsure about the second one.
覆盖等效是什么意思?在这方面, Object
的公共方法有何特殊之处?为什么这不属于第一个标准?
What does it mean by "override-equivalent"? How are public methods of Object
special in this respect? And why is this not covered under the first criterion?
更新说明:这仅适用于Java 7文档。 Java 6 doc 没有提及有关覆盖等效性的任何内容。为什么要改变?
Update note: This is only true of the Java 7 documentation. The Java 6 doc doesn't say anything about override-equivalence. Why the change?
咨询JLS后(第8.4.2节),我发现以下对覆盖等价的解释:
After consulting the JLS (Section 8.4.2), I found the following explanation of override-equivalence:
方法的签名
m1
是方法签名的子签名m2
如果
:
The signature of a method
m1
is a subsignature of the signature of a methodm2
if either:
-
m2
与m1 $具有相同的签名c $ c>,或
-
m1
的签名与删除相同(§4.6))m2 $ c $的签名c>。
m2
has the same signature asm1
, or- the signature of
m1
is the same as the erasure (§4.6) of the signature ofm2
.
两种方法签名 m1
和 m2
覆盖等效 iff m1
是
<$ c $的子签名c> m2 或 m2
是一个副词e m1
。
Two method signatures m1
and m2
are override-equivalent iff either m1
is a
subsignature of m2
or m2
is a subsignature of m1
.
据我所知,这回答了第一个问题(这是什么意思?)和第三个问题(为什么第一个条件不包括这个?)。
As far as I can tell, this answers the first question ("What does it mean?") and the third question ("Why doesn't the first condition cover this?").
如果我理解正确(如果我没有请通知我!),只有一种情况,其中两个方法是覆盖等价的,而不属于原始问题的第一个条件。当子类方法的签名的擦除与超类方法的签名相同时,情况就是这种情况,而不是相反。
If I understand correctly (please inform me if I don't!), there is only one case where two methods are override-equivalent and which doesn't fall under the first condition of the original question. This is the case when the erasure of the signature of the subclass method is the same as the signature of the superclass method, but not the other way around.
当我们在尝试覆盖<$的公共方法时尝试添加类型参数时,原始问题的第二个条件才会发挥作用。 c $ c>对象类。我尝试了以下简单示例来测试它,使用未使用的类型参数:
The second condition of the original question, then, would only come into play when we attempt to add type parameters when attempting to "override" a public method of the Object
class. I tried the following simple example to test this, with an unused type parameter:
public class Foo {
@Override
public <T> boolean equals(Object obj) {
return true;
}
}
当然,这个类不能编译,因为该方法实际上覆盖等于
方法,从而与它发生冲突。但是我仍然收到使用 @Override
注释的错误。假设这是 @Override
用法的第二个条件的有效示例,我错了吗?或者编译器是否生成此错误尽管不需要?
Of course, this class doesn't compile, because the method doesn't actually override the equals
method and thus clashes with it. But I also still receive an error for using the @Override
annotation. Am I wrong in assuming that this is a valid example of the second condition for @Override
usage? Or is the compiler generating this error despite not being required to?
推荐答案
原因是允许您在接口中使用 @Override
注释,这些注释不从 Object
继承,但隐式声明所有公共方法来自对象
(参见 JLS第9.2节接口成员)。因此,您可以声明如下界面:
The reason for this is to allow you to use the @Override
annotation in interfaces, which do not inherit from Object
but implicitly declare all public methods from Object
(see JLS section 9.2 interface members). You are thus allowed to declare an interface like:
interface Bar { @Override int hashCode(); }
但是,您不会被允许声明以下界面:
However, you would not be allowed to declare the following interface:
interface Quux { @Override Object clone(); }
因为 clone()
方法是未在接口中隐式声明(它不是 public
)。
since the clone()
method is not implicitly declared in an interface (it is not public
).
这在 JLS第9.6.3.4节@Override ( @Override
的Javadoc仍然引用旧的节号码。
This is described in JLS section 9.6.3.4 @Override (the Javadoc for @Override
still refers to an old section number)
这篇关于什么是“覆盖等价”它与@Override有什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!