
[历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2013-12-18 标题《Java编程思想》读书笔记:annotation分类编程 / java / think_in_java 标签java·think in java·annotation《Java编程思想》读书笔记annotation几种常见的自定义的annotation说明使用方式说明测试方法结果输出今天重新学习了下Java中的【 annotation 】这里备份备份下代码github【 https://github.com/cstriker1407/think_in_java 】几种常见的自定义的annotationTarget(ElementType.TYPE)//这个标注应用于类Retention(RetentionPolicy.RUNTIME)//标注会一直保留到运行时Documented//将此注解包含在javadoc中Inherited//本注解可以被继承interfaceFirstDesc{publicStringvalue();//value比较特殊它在被指定参数的时候可以不用显示的写出来}Target(ElementType.TYPE)//这个标注应用于类Retention(RetentionPolicy.RUNTIME)//标注会一直保留到运行时Documented//将此注解包含在javadoc中Inherited//本注解可以被继承interfaceSecondDesc{publicStringdes();}Target(ElementType.TYPE)//这个标注应用于类Retention(RetentionPolicy.RUNTIME)//标注会一直保留到运行时Documented//将此注解包含在javadoc中interfaceThirdDesc{publicintid();publicStringdes();}Target(ElementType.TYPE)//这个标注应用于类Retention(RetentionPolicy.RUNTIME)//标注会一直保留到运行时Documented//将此注解包含在javadoc中interfaceFourthDesc{publicintid();publicStringdes()defaultThis is Fourth Desc;}Target(ElementType.METHOD)Retention(RetentionPolicy.RUNTIME)interfaceMethodDesc{publicintid();publicStringdes()defaultThis ia a method desc;}说明从代码中可以看出【 firstdesc 】和【 seconddesc 】可以被继承【 firstdesc 】的【 value 】属性可以在使用注解时省略【 fourthdesc 】有默认值。使用方式FirstDesc(This is a first description)SecondDesc(desThis is a second description)ThirdDesc(id3,desThis is a third description)FourthDesc(id4)staticclassFirstDescClass{}/* * FourthDescClassB 继承了FirstDescClass也继承了 FirstDescClass 的各种注解但是FirstDescClass的各种注解里面 * 只有 FirstDesc SecondDesc设置了Inherited属性因此只有这两个是可以继承的。 */FirstDesc(valueThis is FourthDescClassB a first description)FourthDesc(id4,desthis FourthDescClassB is fourth desc class B)staticclassFourthDescClassBextendsFirstDescClass{}publicclassAnnotationTest{MethodDesc(id0,desthis is test method)publicstaticvoidtest(){}}说明类【 FourthDescClassB 】继承自【 FirstDescClass 】也就会继承【 FirstDescClass 】的各种注解但是只有【 firstdesc 】和【 seconddesc 】允许注解。测试方法publicclassAnnotationTest{MethodDesc(id0,desthis is test method)publicstaticvoidtest(){Annotation[]annArrsFirstDescClass.class.getAnnotations();for(Annotationannotation:annArrs){System.out.println(annotation.toString());}System.out.println();FourthDescfourthDescFourthDescClassB.class.getAnnotation(FourthDesc.class);System.out.println(fourthDesc:idfourthDesc.id() des:fourthDesc.des());System.out.println(fourthDesc.toString());System.out.println();annArrsFourthDescClassB.class.getAnnotations();for(Annotationannotation:annArrs){System.out.println(annotation.toString());}System.out.println();MethodtestMnull;try{testMAnnotationTest.class.getMethod(test,null);annArrstestM.getAnnotations();for(Annotationannotation:annArrs){System.out.println(annotation.toString());}}catch(Exceptione){}}}结果输出yeah.cstriker1407.think_in_java.Annotation.FirstDesc(valueThis is a first description) yeah.cstriker1407.think_in_java.Annotation.FourthDesc(desThis is Fourth Desc, id4) yeah.cstriker1407.think_in_java.Annotation.SecondDesc(desThis is a second description) yeah.cstriker1407.think_in_java.Annotation.ThirdDesc(id3, desThis is a third description) fourthDesc:id4 des:this FourthDescClassB is fourth desc class B yeah.cstriker1407.think_in_java.Annotation.FourthDesc(desthis FourthDescClassB is fourth desc class B, id4) yeah.cstriker1407.think_in_java.Annotation.FirstDesc(valueThis is FourthDescClassB a first description) yeah.cstriker1407.think_in_java.Annotation.FourthDesc(desthis FourthDescClassB is fourth desc class B, id4) yeah.cstriker1407.think_in_java.Annotation.SecondDesc(desThis is a second description) yeah.cstriker1407.think_in_java.Annotation.MethodDesc(desthis is test method, id0)