@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. 前后端分离项目部署到Linux虚拟机

    最近做了一个springboot+vue的前后端分离项目,把它部署到Linux虚拟机上.下面是我的步骤和遇到的问题,需要的朋友可以看下(看的时候注意要全部看完到底部,因为我习惯是把我遇到的问题放到最后 ...

  2. DASCTF Oct吉林工师web

    迷路的魔法少女 进入环境给出源码 <?php highlight_file('index.php'); extract($_GET); error_reporting(0); function ...

  3. Kali Linux 下安装配置MongoDB数据库 ubuntu 下安装配置MongoDB源码安装数据库

    Kali Linux 下安装配置MongoDB数据库   1.下载mongodb.tgz 压缩包: 2.解压到:tar -zxvf mongodb.tgz /usr/local/mongodb 3.创 ...

  4. shell脚本编程(一) 变量、条件判断、循环

    目录   1. shell脚本编程   2. 运行 Shell 脚本有两种方法   3. 变量   4. 本地变量   5. 环境变量   6. 参数变量   7. 多行注释   8. if条件判断 ...

  5. 5V转10.5V原理图

  6. 让你熟知jquery见鬼去吧

    $是jquery最具代表的符号,当然php也是,但是二者不能同日而语;不得不说jquery的选择器是大家赞不绝口的,在它1.x版本中对ie兼容性是最好的,这要归功于$选择器; 现在呢,html5的降临 ...

  7. ES6-11学习笔记--深拷贝与浅拷贝

    Object.assign,只是进行了浅拷贝,并没有进行深拷贝. 而且会在复杂结构当中会丢失属性,如下代码: let target = { a: { b: { c: 3 }, e: 4, f: 5, ...

  8. javaweb之浏览功能

    今天我们来写浏览功能,浏览主要是通过sql语句将数据库里的数据查出来,并显示在页面上. 一.dao层 在上一篇文章的基础上dao层加入浏览方法. public List<Course> l ...

  9. 【uniapp 开发】uni-app 中如何打开外部应用

    我们在开发 App 应用中,经常会遇到打开第三方程序的场景,比如打开手机淘宝.通过第三方浏览器打开一个 url 等等. App不像网页可以使用http超链接互相跳转,但手机os设计了scheme机制, ...

  10. ubantu系统之jdk切换使用

    安装 jdk7: $ sudo apt-get update $ sudo apt-get install openjdk-7-jdk 安装 jdk1.8:sudo add-apt-repositor ...