zoukankan      html  css  js  c++  java
  • 注解

    注解的分类

      1.标准注解

      2.元注解

        @Retention :注解的生命周期

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Retention {
        /**
         * Returns the retention policy.
         * @return the retention policy
         */
        RetentionPolicy value();
    }
    public enum RetentionPolicy {
        /**
         * Annotations are to be discarded by the compiler.
         */
        SOURCE,
    
        /**
         * Annotations are to be recorded in the class file by the compiler
         * but need not be retained by the VM at run time.  This is the default
         * behavior.
         */
        CLASS,
    
        /**
         * Annotations are to be recorded in the class file by the compiler and
         * retained by the VM at run time, so they may be read reflectively.
         *
         * @see java.lang.reflect.AnnotatedElement
         */
        RUNTIME
    }

    SOURCE:只作用在java类中,.class里没有这个注解

    .CLASS:在.class里也会有这个注解

    .RUNTIME:在运行时这个注解也会起到作用.

        @Target :注解的作用目标

          

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Target {
        /**
         * Returns an array of the kinds of elements an annotation type
         * can be applied to.
         * @return an array of the kinds of elements an annotation type
         * can be applied to
         */
        ElementType[] value();
    }
    public enum ElementType {
        /** Class, interface (including annotation type), or enum declaration */
        TYPE,
    
        /** Field declaration (includes enum constants) */
        FIELD,
    
        /** Method declaration */
        METHOD,
    
        /** Formal parameter declaration */
        PARAMETER,
    
        /** Constructor declaration */
        CONSTRUCTOR,
    
        /** Local variable declaration */
        LOCAL_VARIABLE,
    
        /** Annotation type declaration */
        ANNOTATION_TYPE,
    
        /** Package declaration */
        PACKAGE,
    
        /**
         * Type parameter declaration
         *
         * @since 1.8
         */
        TYPE_PARAMETER,
    
        /**
         * Use of a type
         *
         * @since 1.8
         */
        TYPE_USE
    }

     TYPE:作用在类上

     Field:作用在属性上

     Method:作用在方法上

        @Inherited :是否允许之类继承该注解

        @Documented

    自定义注解的实现

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface PersonAnnotation {
        String name();
        int age();
        String gender() default  "";
        String[] language();
    }

    获取注解的信息

    public static void main(String[] args) throws Exception {
            Class clazz = Class.forName("Annotation.Person");
            PersonAnnotation annotation = (PersonAnnotation) clazz.getAnnotation(PersonAnnotation.class);
            System.out.println(annotation.age()+"
    "+annotation.gender()+"
    "+annotation.name());
            for(String str : annotation.language()){
                System.out.println(str+" ");
            }
        }

    通过反射可以获取到注解的信息,但前提是注解的生命周期必须是Runntime.

  • 相关阅读:
    性能战术
    易用性
    可操作性
    软件架构师如何工作
    idea安装
    Windows下安装TensorFlow
    进度十四(11.01)
    进度十三(10.31)
    进度十二(10.30)
    进度十一(10.29)
  • 原文地址:https://www.cnblogs.com/lzh66/p/13526420.html
Copyright © 2011-2022 走看看