java元注解@Inherited的使用详解
作者:lele 发布时间:2023-09-15 04:58:48
标签: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.
*
* <p>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<Student> studentClass = Student.class;
if (studentClass.isAnnotationPresent(InheritedTest.class)){
System.out.println(studentClass.getAnnotation(InheritedTest.class).value());
}
}
}
输出:
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<Student> studentClass = Student.class;
if (studentClass.isAnnotationPresent(IsNotInherited.class)){
System.out.println(studentClass.getAnnotation(IsNotInherited.class).value());
}
}
}
没有输出任何任容,由此可知未拥有@Inherited注解的注解的类的子类不会被继承该注解。
来源:https://segmentfault.com/a/1190000021123395


猜你喜欢
- 一、二进制读写类:1、BinaryReader/BinaryWriter:二进制读写BinaryReader:用特定的编码将基元数据类型读作
- 编写程序,实现顺序表的下列功能:从键盘输入数据建立一个顺序表输出该顺序表往顺序表中插入数据从顺序表中删除数据给定数据,进行查找,给出查找成功
- 如题。3.6 版本的AS,对于活动布局文件的显示有text和view模式,但是切换的按钮位置与之前版本的不同。如下图在右上角的三个按钮点击1
- 背景最近遇到一个有意思的事情,java应用运行在阿里云的ack集群中,某一天有个应用启动突然发现阿里云上的agent都没有注册了,于是开始排
- 本文实例为大家分享了C++实现幸运大抽奖的具体代码,供大家参考,具体内容如下程序效果:#ifndef DIALOG_H#define DIA
- 我们使用控件AutoCompleteTextView 自动提示时,有时需要设置BaseAdapter,设置BaseAdapter时,需要实现
- 本文实例讲述了C#中is和as的用法,对加深is与as用法的理解有一定的帮助借鉴作用。具体如下:现来看个例子:public class Us
- // 举个例子:一个网站有用户系统、商家系统、网站后台3个系统//可以分3个userType, user ,shop , system//网
- java 中死锁问题的实例详解先看代码在做解释public class DeadLock implements Runnable{ &nbs
- 本文实例讲述了C#实现windows form拷贝内容到剪贴板的方法。分享给大家供大家参考。具体实现方法如下:using System;us
- android Notification实例详解1.使用Builder模式来创建2.必须要设置一个smallIcon,还可以设置setTic
- 目录LinkedHashMap 实现继承 LinkedHashMap组合 LinkedHashMap链表 + HashMap 实现LRU,即
- MyBatis插入Insert、InsertSelective的区别逆向自动生成的mybatis对应配置Mapper文件里面,有两个方法,分
- 本文为大家分享了java实现工资管理简单程序的具体代码,供大家参考,具体内容如下程序总体说明(ReadMe):总体分为四部分:管理程序(st
- 一、什么是队列队列是一个有序列表,可以用数组或者链表来实现。遵循先入先出的原则,即:先存入队列的数据,要先取出。后存入的的数据,后取出。看一
- 从功能上说,可以分为两部分,分布式功能和数据功能。分布式功能主要是节点集群及集群附属功能如restful借口、集群性能检测功能等,数据功能主
- 本文实例讲述了C#实现导入CSV文件到Excel工作簿的方法。分享给大家供大家参考。具体如下:你必须在项目中添加对 Microsoft.Of
- 好久没有写文章了,年前公司新开了一个项目,是和usb转串口通信相关的,需求是用安卓平板通过usb转接后与好几个外设进行通信,一直忙到最近,才
- 限流器算法目前常用限流器算法为两种:令牌桶算法和漏桶算法,主要区别在于:漏桶算法能够强行限制请求速率,平滑突发请求,而令牌桶算法在限定平均速
- 1、配置maven环境变量,将maven安装的bin⽬录添加到path路径中(此电脑->属性->高级系统设置->环境变量-