java元注解_java元注解@Inherited的使用详解

2024-01-27 17:20

本文主要是介绍java元注解_java元注解@Inherited的使用详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.先看源码文档

/**

* Indicates that an annotation type is automatically inherited. If

* an Inherited meta-annotation is present on an annotation type

* declaration, and the user queries the annotation type on a class

* declaration, and the class declaration has no annotation for this type,

* then the class's superclass will automatically be queried for the

* annotation type. This process will be repeated until an annotation for this

* type is found, or the top of the class hierarchy (Object)

* is reached. If no superclass has an annotation for this type, then

* the query will indicate that the class in question has no such annotation.

*

*

Note that this meta-annotation type has no effect if the annotated

* type is used to annotate anything other than a class. Note also

* that this meta-annotation only causes annotations to be inherited

* from superclasses; annotations on implemented interfaces have no

* effect.

*

* @author Joshua Bloch

* @since 1.5

* @jls 9.6.3.3 @Inherited

*/

@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.ANNOTATION_TYPE)

public @interface Inherited {

}

上述代码注释部分可以用谷歌翻译大法大致意思是

表示注释类型自动继承。 如果在注释类型声明中存在继承的元注释,并且用户在类声明上查询注释类型,并且类声明没有此类型的注释,则该类的超类将自动查询注释类型。 将重复此过程,直到找到此类型的注释,或者达到类层次结构(Object)的顶部。 如果没有超类具有此类型的注释,则查询将指示所讨论的类没有这样的注释。

请注意,如果使用注释类型来注释除类之外的任何内容,则此元注释类型不起作用。 还要注意,这个元注释只会导致从超类继承注释; 已实现的接口上的注释无效。

通过上述描述可知,使用该注解的注解父类的子类可以继承父类的注解。

2.代码测试

2.1拥有@Inherited注解

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

@Inherited

public @interface InheritedTest {

String value();

}

@InheritedTest("拥有Inherited")

public class Person {

public void method(){

}

public void method2(){

}

}

public class Student extends Person {

}

测试:

public class TestInherited {

public static void main(String[] args) {

Class studentClass = Student.class;

if (studentClass.isAnnotationPresent(InheritedTest.class)){

System.out.println(studentClass.getAnnotation(InheritedTest.class).value());

}

}

}

输出:

c0c1210767f63d639eb52f821269393e.png

2.2未拥有@Inherited注解

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

public @interface IsNotInherited {

String value();

}

@IsNotInherited("未拥有Inherited")

public class Person {

public void method(){

}

public void method2(){

}

}

public class Student extends Person {

}

测试:

public class TestInherited {

public static void main(String[] args) {

Class studentClass = Student.class;

if (studentClass.isAnnotationPresent(IsNotInherited.class)){

System.out.println(studentClass.getAnnotation(IsNotInherited.class).value());

}

}

}

3d534aa4c5ff7d1f6cc148f117886157.png

没有输出任何任容,由此可知未拥有@Inherited注解的注解的类的子类不会被继承该注解。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

这篇关于java元注解_java元注解@Inherited的使用详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/650966

相关文章

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

SQL 中多表查询的常见连接方式详解

《SQL中多表查询的常见连接方式详解》本文介绍SQL中多表查询的常见连接方式,包括内连接(INNERJOIN)、左连接(LEFTJOIN)、右连接(RIGHTJOIN)、全外连接(FULLOUTER... 目录一、连接类型图表(ASCII 形式)二、前置代码(创建示例表)三、连接方式代码示例1. 内连接(I