一、annotation
java.lang.annotation,接口 Annotation。对于Annotation,是Java5的新特性,JDK5引入了Metadata(元数据)很容易的就能够调用Annotations。Annotations提供一些本来不属于程序的数据,比如:一段代码的作者或者告诉编译器禁止一些特殊的错误。An annotation 对代码的执行没有什么影响。Annotations使用@annotation的形式应用于代码:类(class),属性(attribute),方法(method)等等。一个Annotation出现在上面提到的开始位置,而且一般只有一行,也可以包含有任意的参数。
对于Annotation,是Java5的新特性,下面是Sun的Tutorial的描述,因为是英文,这里我翻译下,希望能够比较清晰的描述一下Annotation的语法以及思想。Annotation:Release 5.0 of the JDK introduced a metadata facility called annotations. Annotations provide data about a program that is not part of the program,such as naming the author of a piece of code or instructing the compiler to suppress specific errors. An annotation has no effect on how the code performs. Annotations use the form @annotation and may be applied to a program’s declarations: its classes,fields,methods,and so on. The annotation appears first and often (by convention) on its own line,and may include optional arguments: JDK5引入了Metadata(元数据)很容易的就能够调用Annotations.Annotations提供一些本来不属于程序的数据,比如:一段代码的作者或者告诉编译器禁止一些特殊的错误。An annotation 对代码的执行没有什么影响。Annotations使用@annotation的形式应用于代码:类(class),属性(field),方法(method)等等。一个Annotation出现在上面提到的开始位置,而且一般只有一行,也可以包含有任意的参数。@Author(“MyName”)class myClass() { }
or @SuppressWarnings(“unchecked”)void MyMethod() { }
Defining your own annotation is an advanced technique that won’t be described here,but there are three built-in annotations that every Java programmer should know: @Deprecated,@Override,and @SuppressWarnings. The following example illustrates all three annotation types,applied to methods:
定义自己的Annotation是一个比较高级的技巧,这里我们不做讨论,这里我们仅仅讨论每一个Java programer都应该知道的内置的annotations:@Deprecated,@Override,and @SuppressWarnings。下面的程序阐述了这三种annotation如何应用于methods。import java.util.List;
二、基本用法
//声明Test注解
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Test { }
我们使用了@interface声明了Test注解,并使用@Target注解传入ElementType.METHOD参数来标明@Test只能用于方法上,@Retention(RetentionPolicy.RUNTIME)则用来表示该注解生存期是运行时,从代码上看注解的定义很像接口的定义,确实如此,毕竟在编译后也会生成Test.class文件。对于@Target和@Retention是由Java提供的元注解,所谓元注解就是标记其他注解的注解,下面分别介绍
public enum ElementType { /**标明该注解可以用于类、接口(包括注解类型)或enum声明*/ TYPE, /** 标明该注解可以用于字段(域)声明,包括enum实例 */ FIELD, /** 标明该注解可以用于方法声明 */ METHOD, /** 标明该注解可以用于参数声明 */ PARAMETER, /** 标明注解可以用于构造函数声明 */ CONSTRUCTOR, /** 标明注解可以用于局部变量声明 */ LOCAL_VARIABLE, /** 标明注解可以用于注解声明(应用于另一个注解上)*/ ANNOTATION_TYPE, /** 标明注解可以用于包声明 */ PACKAGE, /** * 标明注解可以用于类型参数声明(1.8新加入) * @since 1.8 */ TYPE_PARAMETER, /** * 类型使用声明(1.8新加入) * @since 1.8 */ TYPE_USE }
转载于:https://www.cnblogs.com/wpj593780933/p/10549899.html
一、annotation相关推荐
- Android -- Annotation(注解)原理详解及常见框架应用
1,我们在上一篇讲到了EventBus源码及3.0版本的简单使用,知道了我们3.0版本是使用注解方式标记事件响应方法的,这里我们就有一个疑问了,为什么在一个方法加上类似于"@Subscrib ...
- 揭开Annotation的面纱
Annotation是Java5.6只后的新特征(中文称之为注解),并且越来越多的得到了应用,比如Spring.Hibernate3.Struts2.iBatis3.JPA.JUnit等等都得到了广泛 ...
- Annotation
在进行类或方法定义的时候,都可以使用一系列的Annotation(public interface Annotation)进行声明,如果想要获取这些Annotation的信息,可以直接通过反射来完成. ...
- JPA相关--Annotation
1.自定义注解 import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java. ...
- 你分析过@Annotation注解的实现原理吗?
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:阿丙 主页:www.cnblogs.com/acm-bing ...
- Hibernate Annotation 学习
1.一对多关联,级联操作 @OneToMany(mappedBy = "paymentad", cascade = CascadeType.ALL, fetch = FetchTy ...
- Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例
摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 公司需要人.产品.业务和方向,方向又要人.产品.业务和方向,方向- 循环』 本文提纲 一 ...
- python使用matplotlib可视化、使用annotate函数以及arrowprops参数在可视化图像中添加箭头和文本注释(arrow and text annotation)
python使用matplotlib可视化.使用annotate函数以及arrowprops参数在可视化图像中添加箭头和文本注释(arrow and text annotation) 目录
- R语言ggplot2在可视化图像中添加横线并在横线中添加文本、为横线中添加的文本添加文本框、自定义文本框的填充色(background color for a text annotation)
R语言ggplot2在可视化图像中添加横线并在横线中添加文本.为横线中添加的文本添加文本框.自定义文本框的填充色(background color for a text annotation) 目录
- seaborn可视化绘制双变量分组条形图(Customizing Annotation of Bars: Side-by-side)、添加数值标签进行标记、并自定义条形图数值标签的格式
seaborn可视化绘制双变量分组条形图(Customizing Annotation of Bars: Side-by-side).添加数值标签进行标记.并自定义条形图数值标签的格式(从45000到 ...
最新文章
- 是谁“偷吃”了硬盘中的3GB空间
- Python必备知识点:对Json的基本使用方法
- CentOS7中怎样安装JDK与配置环境变量
- oracle的SCN和Checkpoint_Change#的关系
- LINUX前期知识回顾
- 图文详解cacti的安装和使用
- javascript数字验证(转)
- 播放器之争:VLC VS SmartPlayer
- 链表的相关面试题(完整)(C语言)
- 51单片机按键输入多位数_单片机实现八路抢答器实例分享
- 与Amnon Shashua的1小时:详解Mobileye自动驾驶进阶之路...
- 计蒜客·中国邮递员问题
- A-B(字符串问题)
- python中string什么意思_Python:string是什么意思
- web端--斗图Tenor api 接入
- 免费的网站,堪称神器
- 如何取得UnityHub内旧版本Unity下载链接
- Unity3D状态机运行状态不显示解决方案哈哈哈
- Python小程序之购买商品
- CSDN博客的文字颜色、字体和字号设置
热门文章
- groupdel 删除组_如何在Linux中删除组– groupdel命令
- sql数据库自动增量备份_SQL自动增量
- sublime text_Sublime Text Editor赠品报告和获胜者
- C++基础知识(三)C++的输入和输出及操纵符
- C Primer Plus (Stephen Prata 著)
- Mobile端Catalog下面Category的配置步骤
- 改变图片局部透明度,实现透明度根据位置不而渐变
- Linux磁盘空间监控告警
- 关于360笔试部分题目小结
- Spring Security的HTTP基本验证示例