java自定義注解學習(二)_注解詳解


上篇文章,我們簡單的實現了一個自定義注解,相信大家對自定義注解有了個簡單的認識,這篇,這樣介紹下注解中的元注解和內置注解

整體圖示

內置注解

@Override 重寫覆蓋

這個注解大家應該經常用到,主要在子類重寫父類的方法,比如toString()方法

package com.kevin.demo;

public class Demo1 {

    @Override
    public String toString(){
        return "demo1";
    }
}

@Deprecated 過時

@Deprecated可以修飾的范圍很廣,包括類、方法、字段、參數等,它表示對應的代碼已經過時了,程序員不應該使用它,不過,它是一種警告,而不是強制性的。

package com.kevin.demo;

public class Demo1 {

    @Deprecated
    public void goHome(){
        System.out.println("過時的方法");
    }
}

idea中調用這些方法,編譯器也會顯示刪除線並警告

@SuppressWarning 壓制Java的編譯警告

@SuppressWarnings表示壓制Java的編譯警告,它有一個必填參數,表示壓制哪種類型的警告.

關鍵字 用途
all to suppress all warnings
boxing to suppress warnings relative to boxing/unboxing operations
cast to suppress warnings relative to cast operations
dep-ann to suppress warnings relative to deprecated annotation
deprecation to suppress warnings relative to deprecation
fallthrough to suppress warnings relative to missing breaks in switch statements
finally to suppress warnings relative to finally block that don¡¯t return
hiding to suppress warnings relative to locals that hide variable
incomplete-switch to suppress warnings relative to missing entries in a switch statement (enum case)
nls to suppress warnings relative to non-nls string literals
null to suppress warnings relative to null analysis
rawtypes to suppress warnings relative to un-specific types when using generics on class params
restriction to suppress warnings relative to usage of discouraged or forbidden references
serial to suppress warnings relative to missing serialVersionUID field for a serializable class
static-access to suppress warnings relative to incorrect static access
synthetic-access to suppress warnings relative to unoptimized access from inner classes
unchecked to suppress warnings relative to unchecked operations
unqualified-field-access to suppress warnings relative to field access unqualified
unused to suppress warnings relative to unused code

上面的方法,我們就可以增加

    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        Demo1 demo1 = new Demo1();
        demo1.goHome();
    }

元注解

元注解:注解的注解,即java為注解開發特准備的注解。

我們以上面講到的java內置注解@Override為例,學習下java元注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}

@Target

@Target表示注解的目標,@Override的目標是方法(ElementType.METHOD),ElementType是一個枚舉,其他可選值有:

  • TYPE:表示類、接口(包括注解),或者枚舉聲明
  • FIELD:字段,包括枚舉常量
  • METHOD:方法
  • PARAMETER:方法中的參數
  • CONSTRUCTOR:構造方法
  • LOCAL_VARIABLE:本地變量
  • ANNOTATION_TYPE:注解類型
  • PACKAGE:包

目標可以有多個,用{}表示,比如@SuppressWarnings@Target就有多個,定義為:

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
    String[] value();
}

如果沒有聲明@Target,默認為適用於所有類型。我們上篇文章的demo就沒有聲明@Target

@Retention

@Retention表示注解信息保留到什么時候,取值只能有一個,類型為RetentionPolicy,它是一個枚舉,有三個取值:

  • SOURCE:只在源代碼中保留,編譯器將代碼編譯為字節碼文件后就會丟掉
  • CLASS:保留到字節碼文件中,但Java虛擬機將class文件加載到內存時不一定會在內存中保留
  • RUNTIME:一直保留到運行時

如果沒有聲明@Retention,默認為CLASS

@Override@SuppressWarnings都是給編譯器用的,所以@Retention都是RetentionPolicy.SOURCE

@Documented

用於指定javadoc生成API文檔時顯示該注解信息。Documented是一個標記注解,沒有成員。

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}

@Inherited

@Inherited 元注解是一個標記注解,@Inherited闡述了某個被標注的類型是被繼承的。

看個栗子

public class Demo1 {

        @Inherited
        @Retention(RetentionPolicy.RUNTIME)
        static @interface Test {
        }

        @Test
        static class Base {
        }

        static class Child extends Base {
        }

        public static void main(String[] args) {
            System.out.println(Child.class.isAnnotationPresent(Test.class));
        }

}

main方法檢查Child類是否有Test注解,輸出為true,這是因為Test有注解@Inherited,如果去掉,輸出就變成false

總結

好了,這篇先學習到這,我要好好看看這些知識,下篇介紹注解的解析啦。好了。玩的開心!

參考


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM