发布于2021-07-13 21:07 阅读(1013) 评论(0) 点赞(28) 收藏(4)
示例一:生成文档相关的注解
示例二:在编译时进行格式检查(JDK内置的三个基本注解)
示例三:跟踪代码依赖性,实现替代配置文件功能
参照@SuppressWarnings定义
如果注解有成员,在使用注解时,需要指明成员的值。
自定义注解必须配上注解的信息处理流程(使用反射)才有意义。
自定义注解通常都会指明两个元注解:Retention、Target
元注解:对现有的注解进行解释说明的注解
@Inherited
@Repeatable(MyAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE,TYPE_PARAMETER,TYPE_USE})
public @interface MyAnnotation {
String value() default "hello";
}
@Test
public void testGetAnnotation() {
Class<Student> studentClass = Student.class;
Annotation[] annotations = studentClass.getAnnotations();
for (int i = 0; i < annotations.length; i++) {
System.out.println(annotations[i]);
}
}
可重复注解:
//jdk8之前的写法:
//@MyAnnotations({@MyAnnotation("hi"),@MyAnnotation("abc")})
@MyAnnotation("hi")
@MyAnnotation("abc")
类型注解:
class Generic<@MyAnnotation T>{
public void show() throws @MyAnnotation RuntimeException{
ArrayList<@MyAnnotation String> list = new ArrayList<>();
int num = (@MyAnnotation int)10L;
}
}
原文链接:https://www.cnblogs.com/koito/p/14887511.html
作者:niceboty
链接:http://www.javaheidong.com/blog/article/246283/5bbbca30d1f76234b509/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!