zoukankan      html  css  js  c++  java
  • 注解

    JDK提供四种元注解

    作用:对现有的注解进行 解释说明的 注解

    • Retention: 指定所修饰的 Annotation 的生命周期:SOURCECLASS(默认)RUNTIME
      只有声明为RUNTIME生命周期的注解,才能通过反射获取。
    • Target:用于指定被修饰的 Annotation 能用于修饰哪些程序元素,例如:可以修饰类,构造器等等
    • Document:表示所修饰的注解在被javadoc解析时,保留下来。
    • Inherited:被他修饰的 Annotation 具有继承性
      例如:
    // 自己写一个注解类
    @Inherited 
    @Retention(value = RetentionPolicy.RUNTIME) //runtime 才能被反射获取到
    public @interface MyAnnotation {
    
        String value(); //表示的成员变量
    }
    
    // Person2类 加上自己的注解
    @MyAnnotation(value = "myTest")
    class Person2{   }
    
    //student2类继承Person2类
    class Student2 extends Person2{   }
    
    //测试,子类继承了父类的注解
         public static void main(String[] args) {
            Student2 student2 = new Student2();
            Annotation[] annotations = student2.getClass().getAnnotations();
            for (Annotation annotation: annotations){
                System.out.println(annotation);   //结果:@com.hyq.java4.MyAnnotation(value=myTest)
            }
        }
    
  • 相关阅读:
    Bridage
    国内项目测试培训笔录和小结
    Proxy
    数据库设计
    PDF转Word
    机务维修成本技术点
    MyEclipse10
    MyEclips:Struts 2 + Hibernate 4 + SQL Server2008
    观察者模式
    javascript事件设计模式
  • 原文地址:https://www.cnblogs.com/huyuqing/p/14353428.html
Copyright © 2011-2022 走看看