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

相关文章

MySQL 中的 JSON 查询案例详解

《MySQL中的JSON查询案例详解》:本文主要介绍MySQL的JSON查询的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 的 jsON 路径格式基本结构路径组件详解特殊语法元素实际示例简单路径复杂路径简写操作符注意MySQL 的 J

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Pandas使用AdaBoost进行分类的实现

《Pandas使用AdaBoost进行分类的实现》Pandas和AdaBoost分类算法,可以高效地进行数据预处理和分类任务,本文主要介绍了Pandas使用AdaBoost进行分类的实现,具有一定的参... 目录什么是 AdaBoost?使用 AdaBoost 的步骤安装必要的库步骤一:数据准备步骤二:模型

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

使用Pandas进行均值填充的实现

《使用Pandas进行均值填充的实现》缺失数据(NaN值)是一个常见的问题,我们可以通过多种方法来处理缺失数据,其中一种常用的方法是均值填充,本文主要介绍了使用Pandas进行均值填充的实现,感兴趣的... 目录什么是均值填充?为什么选择均值填充?均值填充的步骤实际代码示例总结在数据分析和处理过程中,缺失数

Java对象转换的实现方式汇总

《Java对象转换的实现方式汇总》:本文主要介绍Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java对象转换的多种实现方式1. 手动映射(Manual Mapping)2. Builder模式3. 工具类辅助映

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

Python ZIP文件操作技巧详解

《PythonZIP文件操作技巧详解》在数据处理和系统开发中,ZIP文件操作是开发者必须掌握的核心技能,Python标准库提供的zipfile模块以简洁的API和跨平台特性,成为处理ZIP文件的首选... 目录一、ZIP文件操作基础三板斧1.1 创建压缩包1.2 解压操作1.3 文件遍历与信息获取二、进阶技