@Inherited 底层

package java.lang.annotation;

/**
* 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 {
}

举例说明

代码

public class Inheritedtext {
public static void main(String[] args) {
//此时的 A.isAnnotationPresent(aa.class);
//意思就是:注释aa是否在此A上。如果在则返回true;不在则返回false。
System.out.println(A.class.isAnnotationPresent(aa.class));
System.out.println(B.class.isAnnotationPresent(aa.class));
System.out.println(C.class.isAnnotationPresent(aa.class));
System.out.println(D.class.isAnnotationPresent(aa.class));
}
}
//定义注解aa
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@interface aa{}
//被aa注解的类
@aa
class A{
}
class B extends A{
}
//未被aa注解的类
class C {
}
class D extends C{
}

结果

true
true
false
false

效果图



作用

如果一个类用上了@Inherited修饰的注解,那么其子类也会继承这个注解

注意

  1. 接口用上个@Inherited修饰的注解,其实现类不会继承这个注解
  2. 父类的方法用了@Inherited修饰的注解,子类也不会继承这个注解

@Inherited 原注解功能介绍的更多相关文章

  1. 功能:Java注解的介绍和反射使用

    功能:Java注解的介绍和反射使用 一.注解 1.注解介绍 java注解(Annotation),又称为java标注,是jdk5.0引入的一种机制. Java 语言中的类.方法.变量.参数和包等都可以 ...

  2. Java基础笔记 – Annotation注解的介绍和使用 自定义注解

    Java基础笔记 – Annotation注解的介绍和使用 自定义注解 本文由arthinking发表于5年前 | Java基础 | 评论数 7 |  被围观 25,969 views+ 1.Anno ...

  3. HBase的Snapshots功能介绍

    HBase的Snapshots功能介绍 hbase的snapshot功能还是挺有用的,本文翻译自cloudera的一篇博客,希望对想了解snapshot 的朋友有点作用,如果翻译得不好的地方,请查看原 ...

  4. Python中模块之re的功能介绍

    re模块的功能介绍 1. 方法 match 从开头开始查找 方法:re.match(pattern,string,flags=0) 返回值:<class '_sre.SRE_Match'> ...

  5. Python中set的功能介绍

    Set的功能介绍 1.集合的两种函数(方法) 1. 集合的内置函数 交集 格式:x.__and__(y)等同于x&y 例如:s1 = {'a',1,} s2 = {'b',1,} s3 = { ...

  6. Python中dict的功能介绍

    Dict的功能介绍 1. 字典的两种函数(方法) 1. 字典的内置函数 包含关系 格式:x.__contains__(key)等同于key in x 例如:dic = {'ab':23,'cd':34 ...

  7. Python中tuple的功能介绍

    Tuple的功能介绍 1. 元祖的两种方法 1. 元祖的内置方法 两个元祖的相加 格式:x.__add__(y)等同于x+y 例如:tu1 = (1,2,3,) print(tu1.__add__(( ...

  8. Python中list的功能介绍

    List的功能介绍 1. 列表的两种方法 1. 列表的内置方法 列表的相加 格式:x.__add__(y)等同于x+y 例如:list1 = [1,2,3] print(list1.__add__([ ...

  9. Annotation之一:Java Annotation基本功能介绍

    一.元数据的作用 如果要对于元数据的作用进行分类,目前还没有明确的定义,不过我们可以根据它所起的作用,大致可分为三类: 编写文档:通过代码里标识的元数据生成文档.这是最常见的,也是java 最早提供的 ...

随机推荐

  1. idea中Git的配置和Github上推拉项目

    1.去官网下载Git,并且安装它.安装步骤较为简单,此处就不再赘述. 2.在idea中配置Git信息 Git路径就是你电脑中安装Git的位置,找到git.exe文件(正常情况下,idea都会自动给你匹 ...

  2. 遇到的错误之"Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache/velocity/context/Context at"

    问题 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/velocity/context/ ...

  3. 用js中的let等操作,要手动开启ECMAScript6(如果不设置,let等ES6语法会报错)

    问题:idea默认没有开启ECMAScript6,需要进行设置:(如果不设置,let等ES6语法会报错)步骤: File | Settings | Languages & Frameworks ...

  4. C++ | 程序编译连接原理

    文章目录 预编译(生成*.i文件) 编译(生成*.s文件) 汇编(生成*.o文件,也叫目标文件) 链接(生成*.exe文件,也叫可执行文件) 汇编--目标文件 查看文件头 查看符号表 查看 .o 文件 ...

  5. android 布局的android:padding 和android:margin的区别

    android:layout_marginLeft指该控件距离边父控件的边距, android:paddingLeft指该控件内部内容,如文本距离该控件的边距. 如: 当按钮分别设置以上两个属性时,得 ...

  6. ffmpeg将视频生成gif

    1.安装ffmpeg 2.cmd中输入 ffmpeg -i 0.mp4 -f gif 0.gif 即可将视频转为gif

  7. css设置图片根据最大边自适应

    给新手的福利,还是记一下吧.......很简单 <div> <img src="1.jpg" alt=""> </div> ...

  8. C语言---魔方阵

    魔方阵的定义:在n*n的方阵中,每一行的和=每一列的和=对角线的和.(本文中涉及的n为大于3的奇数). 例如3*3的魔方阵为: 5*5的魔方阵为: 如何写魔方阵呢? 1.数字1位于第一行的正中间2.下 ...

  9. python---用顺序表实现队列

    class Queue(object): """队列""" def __init__(self): self.__list = [] def ...

  10. linux磁盘之回环设备

    为什么要讲回环设备,下面看下系统的采样情况 上面图有loop0,loop1等设备标记,这些是什么?看看下面fdisk -l 的结果,乱糟糟的一堆? 什么是回环设备? 上面的loop0, loop1就是 ...