java 自定义注解

来源:互联网 发布:优酷会员淘宝上没得卖 编辑:程序博客网 时间:2024/05/24 15:39

标准元注解:

    @Documented 标记生成javadoc

  @Inherited 标记继承关系

  @Retention 注解的生存期

  @Target 标注的目标

JDK提供了三种保留策略:

@Retention(RetentionPolicy.SOURCE) -- 注解只存在于源代码中,字节码Class文件中将不存在该注解。

@Retention(RetentionPolicy.CLASS) -- 标明注解只会被编译器编译后保留在Class字节码文件中,而运行时无法获取。

@Retention(RetentionPolicy.RUNTIME)  -- 标明注解会保留在class字节码文件中,且运行时能通过反射机制获取。



注解大体上分为三种:标记注解,一般注解,元注解。


用 @Deprecated注解的程序元素,不鼓励程序员使用这样的元素,通常是因为它很危险或存在更好的选择。在使用不被赞成的程序元素或在不被赞成的代码中执行重写时,编译器会发出警告。



@Target 标明注解的作用目标对象。

其作用对象主要有:

JDK源码

public enum ElementType {    <br>  /** Class, interface (including annotation type), or enum declaration */ -- 标注类,接口,注解,枚举<br>    TYPE,     /** Field declaration (includes enum constants) */ --标注字段,枚举常量    FIELD,     /** Method declaration */ -- 标注方法    METHOD,     /** Parameter declaration */ -- 标注参数    PARAMETER,     /** Constructor declaration */--标注构造器     CONSTRUCTOR,     /** Local variable declaration */ -- 标注局部变量    LOCAL_VARIABLE,     /** Annotation type declaration */ -- 标注注解    ANNOTATION_TYPE,     /** Package declaration */ -- 标注包    PACKAGE}

自定义注解:

使用关键字:@interface


0 0