關於註解首先引入官方文檔的一句話:Java 註解用於爲 Java 代碼提供元數據。做爲元數據,註解不直接影響你的代碼執行,但也有一些類型的註解實際上能夠用於這一目的。Java 註解是從 Java5 開始添加到 Java 的。看完這句話也許你仍是一臉懵逼,接下我將從註解的定義、元註解、註解屬性、自定義註解、註解解析JDK 提供的註解這幾個方面再次瞭解註解(Annotation)git
public @interface MyTestAnnotation {
}
複製代碼
@MyTestAnnotation
public class test {
@MyTestAnnotation
public static void main(String[] args){
}
}
複製代碼
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTestAnnotation {
}
複製代碼
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyTestAnnotation {
}
複製代碼
/**自定義註解*/
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyTestAnnotation {
}
/**父類標註自定義註解*/
@MyTestAnnotation
public class Father {
}
/**子類*/
public class Son extends Father {
}
/**測試子類獲取父類自定義註解*/
public class test {
public static void main(String[] args){
//獲取Son的class對象
Class<Son> sonClass = Son.class;
// 獲取Son類上的註解MyTestAnnotation能夠執行成功
MyTestAnnotation annotation = sonClass.getAnnotation(MyTestAnnotation.class);
}
}
複製代碼
/**一我的喜歡玩遊戲,他喜歡玩英雄聯盟,絕地求生,極品飛車,塵埃4等,則咱們須要定義一我的的註解,他屬性表明喜歡玩遊戲集合,一個遊戲註解,遊戲屬性表明遊戲名稱*/
/**玩家註解*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface People {
Game[] value() ;
}
/**遊戲註解*/
@Repeatable(People.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Game {
String value() default "";
}
/**玩遊戲類*/
@Game(value = "LOL")
@Game(value = "PUBG")
@Game(value = "NFS")
@Game(value = "Dirt4")
public class PlayGame {
}
複製代碼
/**註解Repeatable源碼*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Repeatable {
/**
* Indicates the <em>containing annotation type</em> for the
* repeatable annotation type.
* @return the containing annotation type
*/
Class<? extends Annotation> value();
}
複製代碼
/**Annotation接口源碼*/
public interface Annotation {
boolean equals(Object obj);
int hashCode();
Class<? extends Annotation> annotationType();
}
複製代碼
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyTestAnnotation {
String name() default "mao";
int age() default 18;
}
@MyTestAnnotation(name = "father",age = 50)
public class Father {
}
複製代碼
/**是否存在對應 Annotation 對象*/
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
return GenericDeclaration.super.isAnnotationPresent(annotationClass);
}
/**獲取 Annotation 對象*/
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
Objects.requireNonNull(annotationClass);
return (A) annotationData().annotations.get(annotationClass);
}
/**獲取全部 Annotation 對象數組*/
public Annotation[] getAnnotations() {
return AnnotationParser.toArray(annotationData().annotations);
}
複製代碼
public class test {
public static void main(String[] args) throws NoSuchMethodException {
/**
* 獲取類註解屬性
*/
Class<Father> fatherClass = Father.class;
boolean annotationPresent = fatherClass.isAnnotationPresent(MyTestAnnotation.class);
if(annotationPresent){
MyTestAnnotation annotation = fatherClass.getAnnotation(MyTestAnnotation.class);
System.out.println(annotation.name());
System.out.println(annotation.age());
}
/**
* 獲取方法註解屬性
*/
try {
Field age = fatherClass.getDeclaredField("age");
boolean annotationPresent1 = age.isAnnotationPresent(Age.class);
if(annotationPresent1){
Age annotation = age.getAnnotation(Age.class);
System.out.println(annotation.value());
}
Method play = PlayGame.class.getDeclaredMethod("play");
if (play!=null){
People annotation2 = play.getAnnotation(People.class);
Game[] value = annotation2.value();
for (Game game : value) {
System.out.println(game.value());
}
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
}
複製代碼
運行結果:github
註解 | 做用 | 注意事項 |
---|---|---|
@Override | 它是用來描述當前方法是一個重寫的方法,在編譯階段對方法進行檢查 | jdk1.5中它只能描述繼承中的重寫,jdk1.6中它能夠描述接口實現的重寫,也能描述類的繼承的重寫 |
@Deprecated | 它是用於描述當前方法是一個過期的方法 | 無 |
@SuppressWarnings | 對程序中的警告去除。 | 無 |
Java 註解用於爲 Java 代碼提供元數據。做爲元數據,註解不直接影響你的代碼執行,但也有一些類型的註解實際上能夠用於這一目的。數組
/**定義限額註解*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface BankTransferMoney {
double maxMoney() default 10000;
}
/**轉帳處理業務類*/
public class BankService {
/**
* @param money 轉帳金額
*/
@BankTransferMoney(maxMoney = 15000)
public static void TransferMoney(double money){
System.out.println(processAnnotationMoney(money));
}
private static String processAnnotationMoney(double money) {
try {
Method transferMoney = BankService.class.getDeclaredMethod("TransferMoney",double.class);
boolean annotationPresent = transferMoney.isAnnotationPresent(BankTransferMoney.class);
if(annotationPresent){
BankTransferMoney annotation = transferMoney.getAnnotation(BankTransferMoney.class);
double l = annotation.maxMoney();
if(money>l){
return "轉帳金額大於限額,轉帳失敗";
}else {
return"轉帳金額爲:"+money+",轉帳成功";
}
}
} catch ( NoSuchMethodException e) {
e.printStackTrace();
}
return "轉帳處理失敗";
}
public static void main(String[] args){
TransferMoney(10000);
}
}
複製代碼
運行結果:bash
到此,對於Java中註解的解析就結束了。最後,也很是感謝您閱讀個人文章,文章中若是有錯誤,請你們給我提出來,你們一塊兒學習進步,若是以爲個人文章給予你幫助,也請給我一個喜歡和關注,同時也歡迎訪問個人我的博客。框架