基本概念
Annotion (註解)是一個接口,程式可以透過反射來取得指定程式元素的Annotion 對象,然後透過Annotion 物件來取得註解裡面的元資料。
根據註解的使用方法和用途,我們可以將 Annotation 分為三類:系統註解,元註解,自訂註解。
系統註解
系統註解,也就是 JDK 內建的註解,主要有:@Override,@Deprecated,@SuppressWarnnings。
1.@Override
修飾方法時表示方法覆寫了父類別的方法,或實作介面的方法
interface Demo{ public void print(); }public class Test implements Demo{ @Override public void print() { } }
2.@Deprecated
修飾已經過時的方法
3.@SuppressWarnnings
抑制編譯器警告,即移除警告。
常見的參數值有:
名稱 | #作用 |
---|---|
rawtypes | |
#deprecation | |
unchecked | |
fallthrough | |
path | |
#serial | |
finally | |
all |
實例如下:// 抑制单类型@SuppressWarnings("unchecked")public void print() { @SuppressWarnings("rawtypes")
List list = new ArrayList();
list.add("a");
}// 抑制多类型@SuppressWarnings({ "unchecked", "rawtypes" })public void print() {
List list = new ArrayList();
list.add("a");
}// 抑制所有类型@SuppressWarnings({ "all" })public void print() {
List list = new ArrayList();
list.add("a");
}
#元註解
元註解的功能就是負責註解其他註解。 Java5.0 定義了4 個標準的meta-annotation
類型,它們被用來提供對其它 annotation 類型作說明。定義的元註解如下:@Target,@Retention,@Documented,@Inherited。
1.@Target
@Target 定義了Annotation所修飾的物件範圍,具體的修飾範圍如下:public enum ElementType { // 用于描述类、接口(包括注解类型) 或enum声明
TYPE, // 用于描述域(即变量)
FIELD, // 用于描述方法
METHOD, // 用于描述参数
PARAMETER, // 用于描述构造器
CONSTRUCTOR, // 用于描述局部变量
LOCAL_VARIABLE, // 用于描述注解类型
ANNOTATION_TYPE, // 用于描述包
PACKAGE
}
2.@Retention
@Retention 定義了該Annotation 被保留的時間長短,即指明了Annotation 的生命週期。 public enum RetentionPolicy { // 在源文件中有效(编译器要丢弃的注解)
SOURCE, // class 文件中有效(默认,编译器将把注解记录在类文件中,但在运行时 VM 不需要保留注解)
CLASS, // 在运行时有效(编译器将把注解记录在类文件中,在运行时 VM 将保留注解,因此可以反射性地读取)
RUNTIME
}
3.@Documented
#@Documented 定義 Annotation ,表示某一類型的註解將透過 javadoc 和類似的預設工具進行文件化。如果類型宣告是用 Documented 來註解的,則其註解將成為註解元素的公共 API 的一部分。
4.@Inherited
- @Inherited 定義Annotation ,表示註解類型被自動繼承,即一個使用了@Inherited 修飾的annotation 類型被用於一個class,則這個annotation 將會被用於該class的子類別。
- 使用註解類型註解 class 以外的任何事物,@Inherited 都是無效的。
- 此元註解僅促成從父類別繼承註解;對已實作介面的註解無效。
實例如下:// 定义注解@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)@Inherited@interface MyAnotation{
public String name();
}// 作用在类上@MyAnotation(name="parent")
class Parent{
}// 继承 Parent 类public class Test extends Parent{
public static void main(String[] args) {
Class<?> cls = Test.class; // 通过 @Inherited 继承父类的注解
Annotation annotation = cls.getAnnotation(MyAnotation.class);
MyAnotation myAnotation = (MyAnotation) annotation;
System.out.println(myAnotation.name());
}
}// 输出结果:parent(若注释掉注解,返回异常)
自訂註解
1.類別註解
// 定义注解@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)@interface MyAnnotation {
public String name(); public String age();
}// 调用注解@MyAnnotation(name="cook",age="100")public class Test {
public static void main(String[] args) {
Class<?> cls = Test.class; // 1.取得所有注解
Annotation[] annotations =cls.getAnnotations(); // 2.取得指定注解
MyAnnotation annotation =
(MyAnnotation)cls.getAnnotation(MyAnnotation.class);
}
}
2.方法註解
// 定义注解@Retention(RetentionPolicy.RUNTIME)
// 修改作用范围@Target(ElementType.METHOD)@interface MyAnnotation { public String name(); public String age();
}
// 调用注解public class Test {
public static void main(String[] args) throws Exception {
Class cls = Test.class;
Method method = cls.getDeclaredMethod("print", null); // 1.取得所有注解
Annotation[] annotations = method.getDeclaredAnnotations(); // 2.取得指定注解
MyAnnotation annotation =
(MyAnnotation)method.getAnnotation(MyAnnotation.class);
}
3.參數註解
// 定义注解@Retention(RetentionPolicy.RUNTIME)
// 修改作用范围@Target(ElementType.PARAMETER)@interface MyAnnotation { public String name(); public String age();
}public class Test {
public static void main(String[] args) throws Exception {
Class cls = Test.class;
Method method = cls.getDeclaredMethod("print", new Class[]{String.class,String.class});
getAllAnnotations(method);
} // 作用在参数上
public void print(@MyAnnotation(name = "cook", age = "100")
String name, String age) { } public static void getAllAnnotations(Method method) {
Annotation[][] parameterAnnotions = method.getParameterAnnotations();
// 通过反射只能取得所有参数类型,不能取得指定参数
Class[] paraemterTypes = method.getParameterTypes();
int i = 0;
for (Annotation[] annotations : parameterAnnotions) {
Class paraemterType = paraemterTypes[i++];
for (Annotation annotation : annotations) {
if (annotation instanceof MyAnnotation) {
MyAnnotation myAnnotation = (MyAnnotation) annotation;
System.out.println(paraemterType.getName());
System.out.println(myAnnotation.name());
System.out.println(myAnnotation.age());
}
}
}
}
}
#4.變數註解
// 定义注解@Retention(RetentionPolicy.RUNTIME) // 修改作用范围@Target(ElementType.FIELD)@interface MyAnnotation { public String name(); public String age(); }public class Test { // 作用在变量上 @MyAnnotation(name = "cook", age = "100") private String name; public static void main(String[] args) throws Exception { Class cls = Test.class; Field field = cls.getDeclaredField("name"); Annotation[] fieldAnnotions = field.getDeclaredAnnotations(); MyAnnotation annotation = (MyAnnotation) field.getAnnotation(MyAnnotation.class); } }
以上就是09.Java 基礎- 註解的內容,更多相關內容請關注PHP中文網(www.php.cn)! ##########

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。