We define an annotation as an interface as below
我们将注释定义为如下所示的接口
@interface annot_name {
}
and we know that all annotations extends interface java.lang.annotation.Annotation
by default.
我们知道所有的注解都扩展了接口java.lang.annotation。注释默认情况下。
When I checked the java library for Annotation
interface I found that it was overridding many methods of Object class like hashCode()
etc.
当我检查java库的注释接口时,我发现它覆盖了许多对象类的方法,如hashCode()等。
If Annotation is an interface then how could it extend an Object class and override its methods? Interfaces can only extends other interfaces and not classes.
如果注释是一个接口,那么它如何扩展一个对象类并覆盖它的方法呢?接口只能扩展其他接口,而不能扩展类。
1 个解决方案
#1
18
So my question is if Annotation is an interface then how could it extends an Object class and override its methods
我的问题是如果Annotation是一个接口那么它如何扩展一个对象类并覆盖它的方法。
Not exactly. The Java Language Specification §9.2 says
不完全是。Java语言规范§9.2说
If an interface has no direct superinterfaces, then the interface implicitly declares a
public abstract
member methodm
with signatures
, return typer
, andthrows
clauset
corresponding to eachpublic
instance methodm
with signatures
, return typer
, andthrows
clauset
declared inObject
, unless a method with the same signature, same return type, and a compatiblethrows
clause is explicitly declared by the interface.如果一个接口没有直接超接口,然后隐式声明了一个公共的接口抽象成员方法m签名年代,返回类型r,抛出条款t对应于每个公共实例方法m签名年代,返回类型r,并抛出对象中声明的条款t,除非相同的方法签名,相同的返回类型,一个兼容的条款是显式声明的接口。
So any interface
gets those methods.
任何接口都有这些方法。
For Annotation
, the designers just chose to re-declare them in the source code so they could document their behavior.
对于注释,设计人员只是选择在源代码中重新声明它们,以便记录它们的行为。
#1
18
So my question is if Annotation is an interface then how could it extends an Object class and override its methods
我的问题是如果Annotation是一个接口那么它如何扩展一个对象类并覆盖它的方法。
Not exactly. The Java Language Specification §9.2 says
不完全是。Java语言规范§9.2说
If an interface has no direct superinterfaces, then the interface implicitly declares a
public abstract
member methodm
with signatures
, return typer
, andthrows
clauset
corresponding to eachpublic
instance methodm
with signatures
, return typer
, andthrows
clauset
declared inObject
, unless a method with the same signature, same return type, and a compatiblethrows
clause is explicitly declared by the interface.如果一个接口没有直接超接口,然后隐式声明了一个公共的接口抽象成员方法m签名年代,返回类型r,抛出条款t对应于每个公共实例方法m签名年代,返回类型r,并抛出对象中声明的条款t,除非相同的方法签名,相同的返回类型,一个兼容的条款是显式声明的接口。
So any interface
gets those methods.
任何接口都有这些方法。
For Annotation
, the designers just chose to re-declare them in the source code so they could document their behavior.
对于注释,设计人员只是选择在源代码中重新声明它们,以便记录它们的行为。