@Inherited 原注解功能介绍
@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修饰的注解,那么其子类也会继承这个注解
注意
- 接口用上个@Inherited修饰的注解,其实现类不会继承这个注解
- 父类的方法用了@Inherited修饰的注解,子类也不会继承这个注解
@Inherited 原注解功能介绍的更多相关文章
- 功能:Java注解的介绍和反射使用
功能:Java注解的介绍和反射使用 一.注解 1.注解介绍 java注解(Annotation),又称为java标注,是jdk5.0引入的一种机制. Java 语言中的类.方法.变量.参数和包等都可以 ...
- Java基础笔记 – Annotation注解的介绍和使用 自定义注解
Java基础笔记 – Annotation注解的介绍和使用 自定义注解 本文由arthinking发表于5年前 | Java基础 | 评论数 7 | 被围观 25,969 views+ 1.Anno ...
- HBase的Snapshots功能介绍
HBase的Snapshots功能介绍 hbase的snapshot功能还是挺有用的,本文翻译自cloudera的一篇博客,希望对想了解snapshot 的朋友有点作用,如果翻译得不好的地方,请查看原 ...
- Python中模块之re的功能介绍
re模块的功能介绍 1. 方法 match 从开头开始查找 方法:re.match(pattern,string,flags=0) 返回值:<class '_sre.SRE_Match'> ...
- Python中set的功能介绍
Set的功能介绍 1.集合的两种函数(方法) 1. 集合的内置函数 交集 格式:x.__and__(y)等同于x&y 例如:s1 = {'a',1,} s2 = {'b',1,} s3 = { ...
- Python中dict的功能介绍
Dict的功能介绍 1. 字典的两种函数(方法) 1. 字典的内置函数 包含关系 格式:x.__contains__(key)等同于key in x 例如:dic = {'ab':23,'cd':34 ...
- Python中tuple的功能介绍
Tuple的功能介绍 1. 元祖的两种方法 1. 元祖的内置方法 两个元祖的相加 格式:x.__add__(y)等同于x+y 例如:tu1 = (1,2,3,) print(tu1.__add__(( ...
- Python中list的功能介绍
List的功能介绍 1. 列表的两种方法 1. 列表的内置方法 列表的相加 格式:x.__add__(y)等同于x+y 例如:list1 = [1,2,3] print(list1.__add__([ ...
- Annotation之一:Java Annotation基本功能介绍
一.元数据的作用 如果要对于元数据的作用进行分类,目前还没有明确的定义,不过我们可以根据它所起的作用,大致可分为三类: 编写文档:通过代码里标识的元数据生成文档.这是最常见的,也是java 最早提供的 ...
随机推荐
- 学习zabbix(四)
一. 监控知识 1.硬件监控: 机房巡检,远程控制卡,IPMI 2.系统监控: cpu负载查看(指标):uptime(低于3),top(30%-70%), vmstat, mpstat 内存监控: f ...
- mybatis-02-mapper生成器插件使用
sb_mybatis <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- 动态规划 洛谷P1616 疯狂的采药
动态规划 洛谷P1616 疯狂的采药 同样也是洛谷的动态规划一个普及-的题目,接下来分享一下我做题代码 看到题目,没很认真的看数据大小,我就提交了我的代码: 1 //动态规划 洛谷P1616 疯狂的采 ...
- HTML入门学习笔记(二)
第三章 文本 段落 p <p>毫不奇怪,p是最常用到的HTML元素之一</p> 作者联系信息 address address并不是用于标记邮政地址,而是定义与HTML页面或页面 ...
- canvas动画—圆形扩散、运动轨迹
介绍 在ECharts中看到过这种圆形扩散效果,类似css3,刚好项目中想把它用上,but我又不想引入整个echart.js文件,更重要的是想弄明白它的原理,所以自己动手.在这篇文章中我们就来分析实现 ...
- 微信小程序调研
小程序入口 微信发现,小程序 公众号主体查看小程序 好友分享,群分享 公众号自定义菜单跳转 APP页面跳转 第三方服务 附近的小程序 扫普通链接二维码打开小程序 需要后台开启功能,开启后,用户在微信& ...
- H5页面实现下载文件(apk、txt等)的三种方式
需求描述 接到的原始需求是这样的,有一个H5页面,页面中有个"点击下载"的按钮,点击之后,完成下载特定的apk.大概是下面这样的: 需求分析 接到需求的时候我偷乐了一下,这个H5页 ...
- Py的A+B
程序会读入两行,每行都是一个数字,输出这两个数字的和 输入格式: 两行文字,每行都是一个数字 输出格式: 一行数字 输入样例: 18 21 输出样例: 39 代码: a = input() b = i ...
- throws子句在继承当中overrride时有什么规则
8.throws子句在继承当中overrride时的规则 马克-to-win:当子类方法override父类方法时,throws子句不能引进新的checked异常.换句话说:子类override方法的 ...
- Java中if else条件判断语句的执行顺序
学习目标: 掌握 if else 条件判断的使用 学习内容: 1.if语法 if(boolean表达式) { 语句体; } if后面的{}表示一个整体-代码块,称之为语句体,当boolean表达式为t ...