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
了
總結
好了,這篇先學習到這,我要好好看看這些知識,下篇介紹註解的解析啦。好了。玩的開心!
參考
- 1、https://www.cnblogs.com/fsjohnhuang/p/4040785.html
- 2、https://www.cnblogs.com/swiftma/p/6838654.html