日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java中的注解--annotation

發布時間:2023/12/18 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java中的注解--annotation 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  Java中的基本注解,幾乎框架中的注解都是依賴Java中的基本注解,很有必要學習一下Java中的基本注解。。。jdk 5 增加的新特性

一、注解的定義以及Java中常見的注解

注解就相當于一種標記,加了注解,就相當于為程序打上了某中標記,沒加,則相當于沒有這種標記,以后,javac編譯器,開發工具和其他程序可以用反射來了解你的類和各種元素上有五任何標記,看你有何種標記,就去干相應的事,標記可以加在包、類、字段、方法、方法的參數以及局部變量上。

注解相當于一個特殊的類,人家寫好的注解,你才可以引用,帶上注解就相當于引用了一個注解所代表的類,Java中比較常見的幾個注解,比如:

@Override、@SuppressWarnings("deprecation")、@Deprecated

二、自定義注解以及應用

注意:理解元注解的定義以及使用范圍

1 // 這個是元注解 在注解類上的注解 2 @Retention(RetentionPolicy.RUNTIME) 3 // 這個指定在哪些類型上能用 方法、類、屬性還是什么其他的呢 4 // type 是比Class類范圍更加大的一種表示方法 type可以表示類、接口或者是枚舉或者是。。。 5 @Target({ElementType.METHOD,ElementType.TYPE}) 6 public @interface ItcastAnnotation { 7 8 } 9 @ItcastAnnotation 10 public static void main(String[] args) { 11 12 boolean flag = AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class); 13 14 if(flag){ 15 ItcastAnnotation annotation = AnnotationTest.class.getAnnotation(ItcastAnnotation.class); 16 System.out.println(annotation); 17 } 18 19 }

@Retention元注解中的RetentionPolicy有三種枚舉:RetentionPolicy.SOURCE、RetentionPolicy.CLASS、RetentionPolicy.RUNTIME?對應著三個階段?分別是 java源文件 --> class文件 --> 內存中字節碼文件,默認的是RetentionPolicy.CLASS

@Target元注解中ElementType有幾種不同類型的枚舉,意思就是我定義的這個注解可以用到什么地方,是在包上、還是在類上、或者是在方法上、還是屬性、構造方法上,記住這個元注解代表的什么含義就OK了。

?三、為注解增加基本屬性

?注意:在oracle發布的?The?Java? Language Specification java語言規范中規定了屬性的返回值類型可以是:(從中拿過來的)

The return type of a method declared in an annotation type must be one of the following, or a compile-time error occurs:

? A primitive type 基本類型

? String 字符串

? Class or an invocation of Class (§4.5) 類或者是類的調用

? An enum type 枚舉類型

? An annotation type 注解類型

? An array type whose component type is one of the preceding types (§10.1). 以上類型的數組形式

This rule precludes elements with nested array types,such as: //此規則排除具有嵌套數組類型的元素 這種是不可以的

@interface Verboten {

? ? String[][] value();

}

1 // 這個是元注解 在注解類上的注解 2 @Retention(RetentionPolicy.RUNTIME) 3 // 這個指定在哪些類型上能用 方法、類、屬性還是什么其他的呢 4 // type 是比Class類范圍更加大的一種表示方法 type可以表示類、接口或者是枚舉或者是。。。 5 @Target({ElementType.METHOD,ElementType.TYPE}) 6 public @interface ItcastAnnotation { 7 8 //這是一個方法,返回一個字符串,用注解的時候需要設置屬性值 9 String color() default "blue"; 10 11 // 這個是比較特殊的,當注解中只有value屬性時候,引用注解的時候可以只寫數值 12 // @ItcastAnnotation("abc") 這個注解的意思就是注解中只有一個value屬性 13 // 但是你可以類似上面給其他屬性設置默認值 @ItcastAnnotation("abc")的意思就是 @ItcastAnnotation(value="abc",color="blue") 14 String value(); 15 16 // 數組的屬性 17 int[] arrayAttr() default {1,2,3}; 18 19 // 枚舉類型 20 TrafficLamp lamp() default TrafficLamp.RED; 21 22 // 注解類型 返回值是注解類型的屬性值 23 MetaAnnotation annotationAttr() default @MetaAnnotation("111"); 24 } 25 26 // 這個是如何使用自己定義的注解的屬性,以及如何獲取注解的屬性值 27 @ItcastAnnotation(annotationAttr=@MetaAnnotation("112233"),color="red",value="abc",arrayAttr={1,2}) 28 public class AnnotationTest { 29 30 31 @SuppressWarnings("deprecation") 32 // @Override 33 // @Deprecated 34 @ItcastAnnotation("abc") 35 public static void main(String[] args) { 36 37 System.runFinalizersOnExit(true); 38 39 boolean flag = AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class); 40 41 if(flag){ 42 ItcastAnnotation annotation = AnnotationTest.class.getAnnotation(ItcastAnnotation.class); 43 System.out.println(annotation); 44 System.out.println(annotation.color()); 45 System.out.println(annotation.value()); 46 System.out.println(annotation.arrayAttr().length); 47 System.out.println(annotation.lamp().nextLamp().name()); 48 System.out.println(annotation.annotationAttr().value()); 49 } 50 51 } 52 53 }

轉載于:https://www.cnblogs.com/ssh-html/p/10764956.html

總結

以上是生活随笔為你收集整理的Java中的注解--annotation的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。