简体   繁体   English

为什么在Annotation接口中声明了Object的非最终公共方法?

[英]Why non-final public methods of Object declared in Annotation interface?

java.lang.annotation.Annotation interface declares toString , hashCode and equals methods (non-final public methods of Object class) java.lang.annotation.Annotation接口声明toStringhashCodeequals方法(Object类的非最终公共方法)

I think if these methods haven't been declared in Annotation interface, nothing has been changed because these methods already defined in Object class. 我认为,如果尚未在Annotation接口中声明这些方法,则不会更改任何内容,因为这些方法已在Object类中定义。 So we can use them even if they not declared in this interface. 因此,即使未在此接口中声明它们,我们也可以使用它们。 So Why these methods declared in this interface ? 那么,为什么这些方法在此接口中声明呢? What are the necessities of them ? 他们有什么必要?

A quick lookup on SO did not yield any significant duplicate question, so I will post my comment as an answer. 快速浏览SO不会产生任何重大重复的问题,因此我将发表我的评论作为答案。 If there is a duplicate, I happily remove my answer in favour of the duplicate. 如果有重复,我很乐意删除我的答案,赞成重复。

java.lang.annotatino.Annotation is somewhat special: java.lang.annotatino.Annotation有点特殊:

The common interface extended by all annotation types. 由所有注释类型扩展的公共接口。 Note that an interface that manually extends this one does not define an annotation type. 请注意,手动扩展该接口的接口未定义注释类型。

Annotations are not part of the normal object hirachy. 注释不属于普通对象的一部分。 Thus, this interface can be seen as the "root-object" 1 for annotations. 因此,该接口可以被视为注释的“根对象” 1


1 Annotations are not really objects, thus the quotes. 1注释不是真正的对象,因此不是引号。 There is no inheritance within the annotation types, aside from all annotations implicitly implementing the interface java.lang.annotation.Annotation . 除了所有隐式实现接口java.lang.annotation.Annotation注释之外,注释类型内没有继承。

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

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