Java的面向?qū)ο缶幊?/a>">Java的面向?qū)ο缶幊?/a>
1188
2025-04-05
文章目錄
一、自定義注解格式
二、注解本質(zhì)分析
三、注解屬性及類型
四、注解屬性類型
五、注解屬性賦值簡化操作
一、自定義注解格式
分析 Java 中自帶的 @Override 注解 , 源碼如下 :
@Target(ElementType.METHOD) @Retention(RetentionPolicy.SOURCE) public @interface Override { }
1
2
3
4
注解分為兩部分 :
① 元注解 ;
② public @interface 注解名稱 ;
二、注解本質(zhì)分析
按照 public @interface 注解名稱 格式 , 寫出一個注解 , 編譯該注解代碼生成 Annotation.class 字節(jié)碼文件 ;
public @interface Annotation { }
1
2
使用 javap 命令反編譯Annotation.class 字節(jié)碼文件 , 查看該注解的實(shí)際代碼 ;
反編譯命令如下 :
javap Annotation.class
1
輸出內(nèi)容 :
D:
D:\002_Project\004_Java_Learn\Annotation\out\production\Annotation>javap Annotation.class Compiled from "Annotation.java" public interface Annotation extends java.lang.annotation.Annotation { }
2_ProjectD:\002_Project\004_Java_Learn\Annotation\out\production\Annotation>javap Annotation.class Compiled from "Annotation.java" public interface Annotation extends java.lang.annotation.Annotation { }
4_Java_Learn\Annotation\out\production\Annotation>javap Annotation.class Compiled from "Annotation.java" public interface Annotation extends java.lang.annotation.Annotation { }1
2
3
4
注解的本質(zhì)是一個 interface 接口 , 注解接口默認(rèn)繼承了 java.lang.annotation.Annotation 接口 ;
public interface Annotation extends java.lang.annotation.Annotation { }
1
2
三、注解屬性及類型
注解的本質(zhì)是接口 , 接口中可以定義
常量
和
方法 ;
在注解中定義
接口方法
, 就是
注解的屬性 ;
為注解添加屬性 : 接口中的方法都是抽象方法 , 其中 public abstract 可以省略 ;
public @interface Annotation { public abstract String path(); }
1
2
3
注解屬性使用格式 :
@注解名稱(屬性名稱 = 屬性值)
1
注解屬性使用 : 在相關(guān)的代碼上使用
@Annotation(path = "") Student(String name, int age){ }
1
2
3
四、注解屬性類型
注解屬性 ( 接口方法 ) 返回值類型要求 :
①
基本數(shù)據(jù)類型
: byte , short , int , long , float , double , char , boolean ;
②
字符串類型
: String ;
③
枚舉類型
: enum ;
④
注解類型 ;
⑤
以上類型的數(shù)組形式 ;
注解屬性返回值必須是以上的類型 , 不能設(shè)置其它類型返回值 , 否則會報錯 ;
注解中定義了屬性 , 在使用注解時 , 需要 給
注解屬性
賦值 ;
定義 注解屬性 時 , 可以
使用 default 關(guān)鍵字 指定屬性默認(rèn)值
, 下面代碼中 , 制定 注解屬性 intValue 值類型為 int 整型 , 默認(rèn)值 88 ;
int intValue() default 88;
1
如果
注解屬性 指定了默認(rèn)值
, 在使用注解時 , 可以選擇
不為該屬性賦值 ( 此時使用默認(rèn)屬性值 )
,
也可以進(jìn)行賦值 ( 指定一個新的屬性值 ) ;
如果
注解屬性 沒有指定默認(rèn)值
, 則使用 注解 時 ,
必須為其指定一個默認(rèn)值 ,
否則編譯時報錯 ;
數(shù)組類型 的 注解屬性 賦值
時 , 使用大括號進(jìn)行賦值 , 大括號內(nèi)是數(shù)組元素 , 如果只有一個屬性 , 可以省略大括號 ,
注解 聲明示例 :
public @interface Annotation { /** * 字符串類型 * @return */ String stringValue(); /** * int 基本類型 * @return */ int intValue() default 88; /** * 枚舉類型 * @return */ Number enumValue(); /** * 注解類型 * @return */ Annotation2 annotationValue(); /** * 字符串?dāng)?shù)組類型 * @return */ String[] stringArrayValue(); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
枚舉類 :
public enum Number { ONE, TWO, THREE }
1
2
3
Annotation2 注解類 :
public @interface Annotation2 { }
1
2
注解使用示例 :
/** * 注解生成文檔 * * @author hsl * @version 0.1 * @since 1.5 */ public class Student { /** * 構(gòu)造函數(shù) * @param name 參數(shù)一 * @param age 參數(shù)二 */ @Annotation( stringValue = "tom", enumValue = Number.ONE, annotationValue = @Annotation2, stringArrayValue = {"tom", "jerry"}) Student(String name, int age){ } @SuppressWarnings("all") @Override public String toString() { return super.toString(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
代碼分析 : 重點(diǎn)關(guān)注注解的使用 , 使用注解時 , 需要給 沒有默認(rèn)值 的 注解屬性 賦值 , 格式為 注解屬性名稱 = 對應(yīng)類型屬性值 , 如果 注解屬性 有默認(rèn)值 , 則
@Annotation(stringValue = "tom", enumValue = Number.ONE, stringArrayValue = {"tom", "jerry"})
1
五、注解屬性賦值簡化操作
如果
注解屬性 名稱是 value
, 并且
注解中只有 1 1 1 個屬性
, 那么在使用 注解 為 注解屬性 賦值時 , 可以省略注解名稱 , 直接傳入 注解屬性值 ;
示例 : JDK 自帶的 SuppressWarnings 注解 ,
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) @Retention(RetentionPolicy.SOURCE) public @interface SuppressWarnings { String[] value(); }
1
2
3
4
5
注解使用 : 使用 SuppressWarnings 注解時 , 直接傳入 “all” 參數(shù) , 省略了注解屬性名稱 ;
@SuppressWarnings("all") @Override public String toString() { return super.toString(); }
1
2
3
4
5
滿足兩個條件 , 才能使用上述簡化方式 ;
Java
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。