SE:14.Annotation
基本Annotation
@Override
@Deprecated
@SuppressWarnings
@SafeVarargs
@FunctionalInterface
元Annotation
@Retention
@Taget
@Documented
@Inherited
@Repeatable
自定义Annotation
代码里的特殊标记,这些标记可以在编译,类加载,运行时被读取,并执行相应的操作,Annotation是一个接口,程序可以通过反射来获取指定程序元素的Annotation对象,然后通过Annotation对象来取得注解里的元数据
基本Annotation
java提供的5个基本Annotation
@Override
限定重写父类方法,只能修饰方法,不能修饰其他元素
@Deprecated
标记已过时,用于表示某个程序元素(类,方法等)已过时
@SuppressWarnings
抑制编译器警告
@SuppressWarnings(value = "unchecked")public static void method(){List list=new ArrayList();list.add("123");list.add("456");System.out.println(list);}
@SafeVarargs
抑制堆污染警告(堆污染:如把一个不带泛型的对象赋给一个带泛型的变量时,就会发生"堆污染"),主要目的是处理可变长参数中的泛型,此注解告诉编译器:在可变长参数中的泛型是类型安全的。可变长参数是使用数组存储的,而数组和泛型不能很好的混合使用
public static void method(){List stringList=new ArrayList<String>();stringList.add(123);stringList.add(456);List<String> integerList=stringList;System.out.println(integerList.get(0)); }
注意:使用@SafeVarargs注解,对于非static或非final声明的方法,不适用,会编译不通过。
@FunctionalInterface
如果接口中只有一个抽象方法(可以包含多个默认方法或多个static方法),该接口就是函数式接口,@FunctionalInterface就是用来指定某个接口必须是函数式接口,为lambda准备的
元Annotation
主要用来修饰Annotation的定义
@Retention
用于指定被修饰的 Annotation 可以保留多长时间
value成员变量的值只能是如下三个:
- RetentionPolicy.SOURCE:Annotation只保留在源代码中,编译器直接丢弃这种Annotation
- RetentionPolicy.CLASS:编译器将把Annotation记录在class文件中,当运行java程序时,JVM不可获取Annotation信息,这个是默认值
- RetentionPolicy.RUNTIME:编译器将把Annotation记录在class文件中,当运行java程序时,JVM可获取Annotation信息,程序可以通过反射获取该Annotation的信息
//当Annotation的成员变量名为value时,程序中可以直接在Annotation后的括号里指定该成员变量的值,无须使用name=value的形式 @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention {/*** Returns the retention policy.* @return the retention policy*/RetentionPolicy value(); }
@Taget
用于指定被修饰的 Annotation 能用于修饰哪些程序单元
- ElementType.ANNOTATION_TYPE:指定该策略的 Annotation 只能修饰 Annotation
- ElementType.CONSTRUCTOR:指定该策略的 Annotation 只能修饰构造器
- ElementType.FIELD:指定该策略的 Annotation 只能修饰成员变量
- ElementType.LOCAL_VARIABLE:指定该策略的 Annotation 只能修饰局部变量
- ElementType.METHOD:指定该策略的 Annotation 只能修饰方法定义
- ElementType.PACKAGE:指定该策略的 Annotation 只能修饰包定义
- ElementType.PARAMETER:指定该策略的 Annotation 只能修饰参数
- ElementType.TYPE:指定该策略的 Annotation 只能修饰类,接口(包括注解类型)或枚举定义
@Documented
用于指定被该元Annotation修饰的Annotation类将被javadoc工具提取成文档
@Inherited
用于指定被修饰的Annotation将具有继承性---如果某个类使用了@Xxx注解(定义该Annotation时使用了@Inherited修饰)修饰,则其子类将自动被@Xxx修饰
1.定义注解
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Inherited public @interface MyAnnotation { }
2.类定义
@MyAnnotation public class Person { }
public class Student extends Person{ }
3.执行测试&结果
public class Test {public static void main(String[] args) {System.out.println(Student.class.isAnnotationPresent(MyAnnotation.class));} }
@Repeatable
Java8新增的重复注解,重复注解指的是同一个程序元素可以使用多个相同类型的Annotation,定义一个可以重复修饰的注解,使用 @Repeatable 修饰,且需要额外定义一个注解 “容器”
1.定义注解 "容器"
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface MyAnnotations {MyAnnotation[] value(); }
2.定义注解
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Repeatable(MyAnnotations.class) public @interface MyAnnotation {//设置注解的成员变量的默认值String name() default "tony";int age();String[] address(); }
3.使用重复修饰
@MyAnnotation(age = 12, address = {"ShenZhen", "ShangHai"}) @MyAnnotation(age = 13, address = {"ShenZhen", "ShangHai"}) public class Person {}
4.执行测试&结果
public class Test {public static void main(String[] args) {Annotation[] annotations = Person.class.getAnnotations();for(Annotation annotation:annotations){System.out.println(annotation.toString());if(annotation instanceof MyAnnotations){for(MyAnnotation myAnnotation:((MyAnnotations) annotation).value()){System.out.println(myAnnotation.age());System.out.println(myAnnotation.name());System.out.println(myAnnotation.address());}}}} }
自定义Annotation
自定义 Annotation 与接口定义非常像,只是关键字不一样,定义 Annotation 使用 @interface,定义接口使用 interface
1.定义注解
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Inherited public @interface MyAnnotation {//设置注解的成员变量的默认值String name() default "tony";int age();String[] address(); }
2.使用注解修饰
@MyAnnotation(age = 12, address = {"ShenZhen", "ShangHai"}) public class Person {}
3.使用注解
public class Test {public static void main(String[] args) {Annotation[] annotations = Person.class.getAnnotations();for(Annotation annotation:annotations){if(annotation instanceof MyAnnotation){System.out.println(((MyAnnotation)annotation).age());System.out.println(((MyAnnotation)annotation).name());System.out.println(((MyAnnotation)annotation).address());}}} }
注意:如果使用注解时只需要为value成员变量指定值,则可以直接在注解的括号里指定 value 成员变量的值,无须使用(value=变量值)的形式
SE:14.Annotation相关推荐
- 数据结构与算法:14 Leetcode同步练习(五)
Leetcode同步练习(五) 目录 题目01:用栈实现队列 题目02:托普利茨矩阵 题目03:罗马数字转整数 题目04:最长公共前缀 题目05:反转字符串 题目06:无重复字符的最长子串 题目07: ...
- Annotation之一:Java Annotation基本功能介绍
一.元数据的作用 如果要对于元数据的作用进行分类,目前还没有明确的定义,不过我们可以根据它所起的作用,大致可分为三类: 编写文档:通过代码里标识的元数据生成文档.这是最常见的,也是java 最早提供的 ...
- 腾讯创始人马化腾:14年经验得失总结
腾讯创始人马化腾:14年经验得失总结 7月9日,在腾讯合作伙伴大会举办一周年之际,腾讯公司董事会主席兼CEO马化腾向广大合作伙伴发出公开信,就互联网创新.企业管理等话题进行分享.以下为全文: 从 ...
- Java EE与Java SE:Oracle是否放弃了企业软件?
Java Enterprise Edition是全球Java社区中最大的困惑来源之一. 就像<星球大战>和<星际迷航 >之间的区别一样,对于"原力觉醒"是他 ...
- 计算机网络之数据链路层:14、局域网的基本概念
数据链路层:14.局域网的基本概念 思维导图: 局域网: 网络拓扑: 局域网传输介质: 局域网逻辑访问控制方法: 局域网的分类: IEEE802标准: MAC子层和LLC子层: 思维导图: 局域网: ...
- Onvif协议学习:14、球机云台控制PTZ
Onvif协议学习:14.球机云台控制PTZ 文章目录 Onvif协议学习:14.球机云台控制PTZ 一.介绍 二.代码实现 八个方向.放下及缩小控制 聚焦控制 原文链接:https://blog.c ...
- 什么是模型思维?发现问题解决问题之道:14种 MR 模型关系思维模式
在职场或者生活中,我们经常会遇到这样的情况: 在面对和解决一些问题的时候,有些人忙作一团,找不到解决问题的路径.而有些人却可以通过清晰的分析框架一步步解决问题.这样的思维差异,会带来完全不同的结果,不 ...
- 节省 3/4 的设计时间:14个设计神器
1.镜像炫光生成器 weavesilk.com 作为在线生成器的鼻祖之一,Silk 早在2010年就发布了,而且效果至今无人超越. 2.在线自动生成背景图片 coolbackgrounds.io ...
- 阅读论文:Suggestive Annotation: A Deep Active Learning Framework for Biomedical Image Segmentation
阅读论文:Suggestive Annotation: A Deep Active Learning Framework forBiomedical Image Segmentation 本篇论文发表 ...
- 2018年投的龙虾饭外卖店,又凉凉了:14个月,-88.87%
2019年最坏的一年,2019年最好的一年. 多年的投资,在2019年终于先后传来了战报. 上个月,2015年投的海外电商项目-比呀比,关停了,40个月,-100%. 上周,2018年1月投的一个外卖 ...
最新文章
- oracle遇null赋默认值,使用hibernate annotation 为非空列加上默认值
- sgi 之heap, priority_queue
- windows10系统下MongoDB的安装及环境配置
- php左侧,php左侧补零
- android抓包工具——使用fiddler4在安卓手机抓包
- 简单了解static
- win10设置默认输入法_个性化设置技巧
- 一名老程序员的一点感悟给未来的程序员
- 古董笔记本伪装成双核 1G内存 120G硬盘
- 寻路算法:找到NPC最好的行走路径
- 【随机过程】随机过程之更新过程(1)
- qt mdi 子窗口关闭再打开_QT 信号的使用方法
- 点云高斯曲率计算(MATLAB)
- HTML中绑定点击事件的方式
- java barchart_Bar Chart
- java将bmp文件转为jpg_在PHP中将BMP转换为JPG
- 【JavaSE】自定义异常
- 在蹉跎中一路前行---谈Microsoft .NET战略
- 计算机应用和维护发展方向,计算机应用技术的现状及前景
- Unity3D学习 ③ 摄像机视角跟随
热门文章
- 克罗内克张量积 Kron 的 OpenCV C++实现
- Caused by: org.hibernate.AssertionFailure: Subclass has to be binded after it's mother class:
- latex表格排版指南
- 计算机主板cpu的电源接口类型,给力:主板CPU电源的4pin和8pin有什么区别?
- 相机靶面尺寸详解+工业相机选型
- JAVA向word模板文档中写入数据并下载
- 数据科学 IPython 笔记本 8.16 地理数据和 Basemap
- HttpServletRequest获取中文参数乱码问题
- Java递归求费数列和_java – 斐波纳契数列 – 递归求和
- 数仓建模—数据资产管理