黑马程序员技术交流社区

标题: 整理了下注解,求赐教。 [打印本页]

作者: homeless    时间: 2014-3-11 18:13
标题: 整理了下注解,求赐教。
/**
* 注解(Annotation) 主要是在代码中添加信息,以方便在需要的时候使用。
*主要作用:
* 1.生成文档:是java 最早提供的注解。比如@see @param @retur
* 2.替代配置文。比较常见的是spring 2.5 开始的基于注解配置。作用就是减少配置
* 3.在编译时进行格式检查。如@override 放在方法前,如果你这个方法并不是覆盖了超类方法,则编译时就能检查出。
*
*四个元注解,Documented,Inherited,Target,Retentio。用声明注解本身要具有的功能。
*Inherited :预设上父类别中的注解并不会被继承至子类别中,加上Inherited 限定后,注解被继承下来。
*Target:定义注解在空间上的作用范围,可以被添加在哪些类型的元素上,如类型、方法和域等
*Retentio:定义注解的保留策略,在时间上的作用范围,有source class runtime三种


*/

//@Target({ElementType.TYPE,ElementType.FIELD}) //当需要作用于多个成员上时的形式
@Retention(RetentionPolicy.RUNTIME)
public @interface YevxAnnotation {
        /*@interface用来声明一个注解,它将自动继承Annotation。
         * {}内的每一个方法实际上是声明了一个配置参数。
         * 方法的名称就是参数的名称,返回值类型就是参数的类型
         * (返回值类型只能是基本类型、Class、String、enum)。
         * 可以通过default来声明参数的默认值。*/
       
        String name();
        int id() default 0;
        //int[] arr() default {1,2,4};//数组只有一个元素时,可写成arr=1,其它arr={111,22,33}
        //String value();//这个比较特别,当它单独存在时,可以省去方法名。如@Yev("gegege")等价于@Yev(value="gegege")
}
//把注解应用到类,可以有多个。
@axc1(a="hahah")
@YevxAnnotation(name="类",id=1)    //使用了类注解
public class AnnotationTest {
        @YevxAnnotation(name="字段",id=2) //使用了类成员注解
            public int age;//这里本来是private,我取不出来,我就给改了。
            
        @YevxAnnotation(name="构造器",id=3) //使用了构造方法注解
            public AnnotationTest(){
                
            }
        @YevxAnnotation(name="方法show()",id=4)//使用了类方法注解
            public void show(){
                @YevxAnnotation(name="局部",id=5) //使用了局部变量注解
                String s="hahahha";
            }
            
            public void xxx(@YevxAnnotation(name="参数",id=6) int age){ //使用了方法参数注解
                
            }
         
//把注解取出来   
            public static void main(String[] args)throws Exception{
                        //提取类上用到的所有注解(我定义了三个注解,类上放了2个,另一个放内部,得出的数组大小是2,他们的策略都是runtime)
                        Class clazz=Class.forName("com.day2.AnnotationTest");//拿到应用了注解的Class对象
                         Annotation[] annotations = clazz.getAnnotations();//通过class对象获取类上所有的注解
                         System.out.println("length"+annotations.length);
//                         for(Annotation annotation:annotations){//迭代打印。
//                                 printAnnoID(annotation);
//                         }
                         //提取方法上的注解。先得拿到方法
                         Method method=clazz.getMethod("show");
                         //判断方法上是否有注解
                         boolean ishas=method.isAnnotationPresent(YevxAnnotation.class);
                         if(ishas){
                                 Annotation annotation=method.getAnnotation(YevxAnnotation.class);
                                 printAnnoID(annotation);
                         }
                         //字段注解,都一个道道,差不多。
                         Field field=clazz.getField("age");//age是private 时,我取不到注解,是我不会取,还是不能取??
                         
                         ishas=field.isAnnotationPresent(YevxAnnotation.class);
                         if(ishas){
                               
                                 Annotation annotation=field.getAnnotation(YevxAnnotation.class);
                                 printAnnoID(annotation);
                         }
                }
                private static void printAnnoID(Annotation annotation) {
                        System.out.println(((YevxAnnotation)annotation).id()+((YevxAnnotation)annotation).name());
                }
                 
}






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2