寫在前面
關於java註解的基礎內容這裏就不多進行解釋,直接演示工作中註解不經常使用到的功能
Inherited說明
在註解上註解上@Inherited的作用是什麼呢?從字面意思可以知道是繼承,具體作用直接代碼演示
不使用Inherited註解的情況
- 首先定義一個註解Littlehow,註解上先不加Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface Littlehow {
String value();
}
- 定義測試類
public class Test {
public static void main(String[] args) {
Annotation[] annotations = BB.class.getAnnotations();
for (Annotation annotation : annotations) {
Class type = annotation.annotationType();
if (Littlehow.class.equals(type)) {
System.out.println(((Littlehow) annotation).value());
}
}
}
}
@Littlehow("this littlehow is AA")
class AA {
}
class BB extends AA {
}
運行測試類Test發現BB上並沒有出現註解Littlehow
使用Inherited的情況(一)
- 首先定義一個註解Littlehow,加Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Inherited
public @interface Littlehow {
String value();
}
- 測試類與【不使用Inherited註解的情況】的測試類保持一致
運行將輸出
this littlehow is AA
使用Inherited的情況(二)
-
Littlehow註解與【使用Inherited的情況(一)】的Littlehow註解保持一致
-
測試類如下:
public class TestB {
public static void main(String[] args) {
Annotation[] annotations = BB.class.getAnnotations();
for (Annotation annotation : annotations) {
Class type = annotation.annotationType();
if (Littlehow.class.equals(type)) {
System.out.println(((Littlehow) annotation).value());
}
}
}
}
@Littlehow("this littlehow is AA")
class AA {
}
/**
* 此時BB註解上加上了註解Littlehow
*/
@Littlehow("this littlehow is BB")
class BB extends AA {
}
運行將輸出
this littlehow is BB
小結
註解Inherited表示註解的繼承性,
1.當子類無註解時,會繼承父類的註解;
2.當子類有註解時,將使用子類自己的註解;
Repeatable說明
在註解上註解上@Repeatable的作用是什麼呢?從字面意思可以知道是重複,具體作用直接代碼演示
不使用Repeatable註解的情況
-
Littlehow註解與【使用Inherited的情況(一)】的Littlehow註解保持一致
-
定義測試類
public class Test {
public static void main(String[] args) {
Annotation[] annotations = AA.class.getAnnotations();
for (Annotation annotation : annotations) {
Class type = annotation.annotationType();
if (Littlehow.class.equals(type)) {
System.out.println(((Littlehow) annotation).value());
}
}
}
}
@Littlehow("this littlehow is AA1")
@Littlehow("this littlehow is AA2")
class AA {
}
編譯異常,無法進行編譯,提示Littlehow不是一個合法的java.lang.annotation.Repeatable註解
使用Repeatable的情況(無Inherited)
- 定義註解(這裏需要定義兩個註解)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
//當有重複時,使用哪個註解來進行重複註解的收錄
@Repeatable(Littlehows.class)
public @interface Littlehow {
String value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface Littlehows {
Littlehow[] value();
}
- 定義測試類
public class Test {
public static void main(String[] args) {
Annotation[] annotations = AA.class.getAnnotations();
for (Annotation annotation : annotations) {
Class type = annotation.annotationType();
if (Littlehow.class.equals(type)) {
System.out.println(((Littlehow) annotation).value());
} else if (Littlehows.class.equals(type)) {
System.out.println("進入重複註解中");
Littlehows littlehows = (Littlehows) annotation;
for (Littlehow littlehow : littlehows.value()) {
System.out.println(littlehow.value());
}
}
}
}
}
@Littlehow("this littlehow is AA1")
@Littlehow("this littlehow is AA2")
class AA {
}
運行將輸出:
進入重複註解中
this littlehow is AA1
this littlehow is AA2
使用Repeatable的情況(有Inherited)
- 定義註解(這裏需要定義兩個註解)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Inherited
//當有重複時,使用哪個註解來進行重複註解的收錄
@Repeatable(Littlehows.class)
public @interface Littlehow {
String value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Inherited
public @interface Littlehows {
Littlehow[] value();
}
- 定義測試類(父類兩個相同註解)
public class Test {
public static void main(String[] args) {
Annotation[] annotations = BB.class.getAnnotations();
for (Annotation annotation : annotations) {
Class type = annotation.annotationType();
if (Littlehow.class.equals(type)) {
System.out.println("進入單個註解中");
System.out.println(((Littlehow) annotation).value());
} else if (Littlehows.class.equals(type)) {
System.out.println("進入重複註解中");
Littlehows littlehows = (Littlehows) annotation;
for (Littlehow littlehow : littlehows.value()) {
System.out.println(littlehow.value());
}
}
}
}
}
@Littlehow("this littlehow is AA1")
@Littlehow("this littlehow is AA2")
class AA {
}
@Littlehow("this littlehow is BB")
class BB extends AA {
}
運行將輸出:
進入重複註解中
this littlehow is AA1
this littlehow is AA2
進入單個註解中
this littlehow is BB
- 定義測試類,重寫AA類(父類一個相同註解)
@Littlehow("this littlehow is AA")
class AA {
}
@Littlehow("this littlehow is BB")
class BB extends AA {
}
運行將輸出:
進入單個註解中
this littlehow is BB
小結
Repeatable作用爲定義可重複註解的註解,當同一個類上有相同註解時,將合併爲一個複合註解;當設置可繼承時,子類和父類各一個可重複註解不會被合成;
Repeatable作用的時機:當同一個類上同時註解多個可重複註解時,在編譯時就會將其合併;所以運行時獲取到的是複合註解。
寫在後面
littlehow 寫於 2020/05/29