今天重新学习了下Java中的【 annotation 】,这里备份备份下代码:
github:
【 https://github.com/cstriker1407/think_in_java 】
几种常见的自定义的annotation:
@Target (ElementType.TYPE) |
@Retention (RetentionPolicy.RUNTIME) |
@Target (ElementType.TYPE) |
@Retention (RetentionPolicy.RUNTIME) |
@Target (ElementType.TYPE) |
@Retention (RetentionPolicy.RUNTIME) |
@Target (ElementType.TYPE) |
@Retention (RetentionPolicy.RUNTIME) |
public String des() default "This is Fourth Desc" ; |
@Target (ElementType.METHOD) |
@Retention (RetentionPolicy.RUNTIME) |
public String des() default "This ia a method desc" ; |
说明:
从代码中可以看出,【 firstdesc 】和【 seconddesc 】可以被继承,【 firstdesc 】的【 value 】属性可以在使用注解时省略,【 fourthdesc 】有默认值。
使用方式:
@FirstDesc ( "This is a first description" ) |
@SecondDesc (des= "This is a second description" ) |
@ThirdDesc (id= 3 ,des= "This is a third description" ) |
static class FirstDescClass |
@FirstDesc (value= "This is FourthDescClassB a first description" ) |
@FourthDesc (id= 4 ,des= "this FourthDescClassB is fourth desc class B" ) |
static class FourthDescClassB extends FirstDescClass |
public class AnnotationTest |
@MethodDesc (id= 0 ,des= "this is test method" ) |
public static void test() |
说明:
类【 FourthDescClassB 】继承自【 FirstDescClass 】,也就会继承【 FirstDescClass 】的各种注解,但是只有【 firstdesc 】和【 seconddesc 】允许注解。
测试方法:
public class AnnotationTest |
@MethodDesc (id= 0 ,des= "this is test method" ) |
public static void test() |
Annotation[] annArrs = FirstDescClass. class .getAnnotations(); |
for (Annotation annotation : annArrs) |
System.out.println(annotation.toString()); |
System.out.println( "==" ); |
FourthDesc fourthDesc = FourthDescClassB. class .getAnnotation(FourthDesc. class ); |
System.out.println( "fourthDesc:id=" + fourthDesc.id() + " des:" + fourthDesc.des()); |
System.out.println(fourthDesc.toString()); |
System.out.println( "==" ); |
annArrs = FourthDescClassB. class .getAnnotations(); |
for (Annotation annotation : annArrs) |
System.out.println(annotation.toString()); |
System.out.println( "==" ); |
testM = AnnotationTest. class .getMethod( "test" , null ); |
annArrs = testM.getAnnotations(); |
for (Annotation annotation : annArrs) |
System.out.println(annotation.toString()); |
结果输出:
@yeah.cstriker1407.think_in_java.Annotation.FirstDesc(value=This is a first description) |
@yeah.cstriker1407.think_in_java.Annotation.FourthDesc(des=This is Fourth Desc, id=4) |
@yeah.cstriker1407.think_in_java.Annotation.SecondDesc(des=This is a second description) |
@yeah.cstriker1407.think_in_java.Annotation.ThirdDesc(id=3, des=This is a third description) |
fourthDesc:id=4 des:this FourthDescClassB is fourth desc class B |
@yeah.cstriker1407.think_in_java.Annotation.FourthDesc(des=this FourthDescClassB is fourth desc class B, id=4) |
@yeah.cstriker1407.think_in_java.Annotation.FirstDesc(value=This is FourthDescClassB a first description) |
@yeah.cstriker1407.think_in_java.Annotation.FourthDesc(des=this FourthDescClassB is fourth desc class B, id=4) |
@yeah.cstriker1407.think_in_java.Annotation.SecondDesc(des=This is a second description) |
@yeah.cstriker1407.think_in_java.Annotation.MethodDesc(des=this is test method, id=0) |
发表评论