zoukankan      html  css  js  c++  java
  • 31注解

    注解就是给计算机看的注释

    Invalid type Object for the annotation attribute First.object; only primitive type, String, Class, annotation, enum are permitted or 1-dimensional arrays thereof 说明支持的属性范围是 基本数据类型,String,Class,注解,枚举,以及对应形式的一位数组表现形式。

    注解可以加载类上,方法上,方法参数上,行为太多了。很容易乱,所以要用元注解来限制注解的行为

    元注解就是给注解的注解

    1.@Target ----控制注解可以在什么位置使用

    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
    }

    2.@Retention 控制注解的级别,什么时候生效

    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 //运行时期
    }

    3.@Documented---可以让注解随着类一起问单独给出去

    4.@Inherited ---可以让子类依然具有注解

  • 相关阅读:
    冒泡排序
    获取某年某月有多少天 & 常用日期转换
    left join,right join ,inner join
    Left join加上where条件的困惑
    ORACLE查询练习
    Ch25 文件和注册表操作(2)-- 读写文件
    Ch25 文件和注册表操作(1)--文件系统
    C#入门经典札记_Ch05变量的更多内容
    C#入门经典札记_Ch04流程控制
    C#入门经典札记_Ch03变量和表达式
  • 原文地址:https://www.cnblogs.com/xuwangqi/p/11264571.html
Copyright © 2011-2022 走看看