Annotation

1.     annotation的保留策略 :  @Retation(...)                参数是一个枚举类型:RetationPolicy          (源文件、CLASS文件(默认)、运行时)

    只有策略设置成RUNTIME时,才可以运行时期通过反射读取Annotation

 

2.  annotation的目标:      @Target(...)                  参数是一个枚举类型:ElementType           (包、类、构造函数、方法、变量、字段、、、)

 

3.  @Retation@Target 是 用来修饰我们自定义AnnotationAnnotation(元Annotation),都可以在运行时期,通过反射读取,

        所以 它俩的 @Retation都是:RUNTIME        @Target都是:ANNOTATION_TYPE

 

4.  jdk1.5自带3个常用的Annotation

@Override :表示子类重写父类的方法,Taget是方法级别,策略是Source

@SuppressWarnning:表示取消警告,策略是Source

@Deprecated:表示已经过时,      TargetRuntime运行时,策略是所有。

 

5.  annotation中可以有变量,但是变量类型只能是:

    18种原生数据类型、stringclassannotationenum

    2)上述类型的一维数组

 

自定义Annotation实例
@interface MyAnnotation2
{
        String name();
}

@Retention(RetentionPolicy.RUNTIME)
@Target(value
={ElementType.TYPE, ElementType.METHOD})
public @interface MyAnnotation
{
        String name() 
default "zhaoql";
        MyAnnotation2 ann() 
default @MyAnnotation2(name="nami");
}

 

 

 

posted on 2010-09-21 03:18  TroyZ  阅读(399)  评论(0编辑  收藏  举报