Java中的註解入門

註解是在Java SE5中引入的,Java內置了三種註解:
  @Override:子類重寫父類方法時用。
  @Deprecated:不贊成使用該元素(在Android中經常能看到某些類或方法Deprecated了)。
  要注意@Deprecated與@deprecated的區別,@deprecated是爲了生成文檔的需要,具體如下所示:

/**
 * @deprecated 該方法不建議使用
 */
@Deprecated
public void method() {

}

  @SuppressWarnings:你覺得編譯器的某些警告是沒必要的,就可以用它了,不過它需要需要添加一個參數,我們下面看個用的比較多的unchecked:

@SuppressWarnings("unchecked")
public void method() {
    List list = new ArrayList<>();
    list.add("qwe");
}  

  下面我們來看下怎麼自定義註解。
  先看個最簡單的,沒有一個元素,被稱爲標記註解:

@MyAnnotation
public void method() {

}

@interface MyAnnotation {
}  

  下面看下帶元素的註解:

@MyAnnotation(m = 5.6, m2 = "dsac")
public void method() {
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
    double m();
    String m2();
    int m3() default 3;
}  

  大家有沒有注意到上面的@Target和@Retention,它們叫元註解,相當於註解的註解,另兩個是@Documented和@Inherited,具體含義如下:
  @Target:限定註解可以修飾元素的種類,如果不加,註解既可以修飾類,也可以修飾變量,方法。上面的代碼中我們限定了註解只能修飾方法。其中ElementType是個枚舉類,源代碼如下:

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
}  

  @Retention:將註解分爲三級。
  SOURCE級,註解只存在源碼中,會被編譯器丟棄,如@Override,@SuppressWarnings之類的,比如Android中的註解式框架Dagger用的就是這個級別的註解。
  CLASS級,註解存在於源碼,字節碼中,會被VM丟棄。
  RUNTIME級:註解存在於源碼,字節碼,VM中,可用於運行時反射獲取信息。
  其中RetentionPolicy也是個枚舉類,源代碼如下:

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
}

  
  @Documented:被修飾的註解會生成到Javadocs中,具體用javadoc命令生成看下就行了。
  
  @Inherited:允許子類去繼承父類的註解,具體看下面的例子,

package com.company;


import java.lang.annotation.*;
import java.util.Arrays;

public class Test4 {

    public static void main(String[] args) {

        A b = new B();
        System.out.println(Arrays.toString(b.getClass().getAnnotations()));

    }

}

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@interface MyInheritedAnnotation {
}


@Retention(RetentionPolicy.RUNTIME)
@interface MyUnInheritedAnnotation {
}

@MyInheritedAnnotation
@MyUnInheritedAnnotation
class A {

}

class B extends A {

}

  輸出爲:

[@com.company.MyInheritedAnnotation()]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章