java - 为什么在 Annotation 接口(interface)中声明 Object 的非最终公共(public)方法?

标签 java interface annotations

java.lang.annotation.Annotation 接口(interface)声明 toStringhashCodeequals 方法(非最终版本) Object 类的公共(public)方法)

我认为如果这些方法没有在Annotation接口(interface)中声明,那么什么都没有改变,因为这些方法已经在Object类中定义了。因此,即使它们没有在此接口(interface)中声明,我们也可以使用它们。那么为什么要在这个接口(interface)中声明这些方法呢? 他们的必需品是什么?

最佳答案

对 SO 的快速查找没有产生任何重要的重复问题,因此我将发布我的评论作为答案。如果有重复,我很乐意删除我的答案以支持重复。

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.

注释不是普通对象 hirachy 的一部分。因此,该接口(interface)可以被视为注释的“根对象”1

<小时/>

1 注释并不是真正的对象,因此引号也是如此。除了隐式实现接口(interface) java.lang.annotation.Annotation 的所有注释之外,注释类型中没有继承。

关于java - 为什么在 Annotation 接口(interface)中声明 Object 的非最终公共(public)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49813989/

相关文章:

java - @PreAuthorize注解码合

java - Spring-JSF2 集成 : Target Unreachable, 标识符 'customerBean' 解析为空

java - 在XStream中使用JavaBeanConverter时如何使用@Transient注解?

java - 在适配器和 fragment 中获取正确的上下文

java - 上传文件 Android org.apache.http.entity.mime.MultipartEntity : Bad Request Error

java - 了解 Java 接口(interface)?

java - 如何在 Eclipse 中查找给定类的哪些子类实现了特定接口(interface)?

go - Go接口(interface)值是实现接口(interface)的整个变量吗?

java - 避免 @onetomany join 中不必要的提取

java - 我应该在 java 中的方法中使用哪个访问修饰符?