成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

女朋友說想要自己的注解,我又活下來了!!!

開發 前端
你spring學的不錯,那我先帶你參觀下Autowired吧~看到 「Autowired」 發現,這個類的「類名就叫 Autowired」,所以你知道為什么貼的是 @Autowired 了嗎?

[[416903]]

女朋友:我想要我自己的注解,你教我!

moon:誒?你怎么突然想要自己的注解了?

女朋友:關你什么事!「分手」!

moon:別別別別別!我教!

moon:看好了,我的寶~,你spring學的不錯,那我先帶你參觀下Autowired吧~

  1. @Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE}) 
  2. @Retention(RetentionPolicy.RUNTIME) 
  3. @Documented 
  4. public @interface Autowired { 
  5.  /** 
  6.   * Declares whether the annotated dependency is required. 
  7.   * <p>Defaults to {@code true}. 
  8.   */ 
  9.  boolean required() default true

moon:看到 「Autowired」 發現,這個類的「類名就叫 Autowired」,所以你知道為什么貼的是 @Autowired 了嗎?

女朋友:哦哦哦哦哦哦!我懂了!原來「類名就是注解名」!

moon:我女朋友就是聰明!我們再來看看,它還有一點比較特殊的地方,類的標志是 class,而「注解的標志是 @interface」。

女朋友:嗯.....不錯不錯,你繼續

moon:我們再來看下 @Autowired 上面還有三個注解,分別是什么作用,先來看第一個 「@Documented」

  1. /** 
  2.  * Indicates that annotations with a type are to be documented by javadoc 
  3.  * and similar tools by default.  This type should be used to annotate the 
  4.  * declarations of types whose annotations affect the use of annotated 
  5.  * elements by their clients.  If a type declaration is annotated with 
  6.  * Documented, its annotations become part of the public API 
  7.  * of the annotated elements. 
  8.  * 
  9.  * @author  Joshua Bloch 
  10.  * @since 1.5 
  11.  */ 
  12. @Documented 
  13. @Retention(RetentionPolicy.RUNTIME) 
  14. @Target(ElementType.ANNOTATION_TYPE) 
  15. public @interface Documented { 

moon:看,我們發現了,第一個是 @Documented,我們來看看它的注釋是什么?

圖片

moon:通過我強大的英文閱讀能力,發現 「@Documented 注解其實只是用來生成文檔的」,使用 javadoc 就可以生成 api 文檔了,所以這個注解,肯定「不重要」

女朋友:呸!你明明是靠翻譯的!學渣!

moon:嘿嘿,我們再來看下一個!「@Retention」!這個可有的說頭了。

  1. /** 
  2.  * Indicates how long annotations with the annotated type are to 
  3.  * be retained.  If no Retention annotation is present on 
  4.  * an annotation type declaration, the retention policy defaults to 
  5.  * {@code RetentionPolicy.CLASS}. 
  6.  * 
  7.  * <p>A Retention meta-annotation has effect only if the 
  8.  * meta-annotated type is used directly for annotation.  It has no 
  9.  * effect if the meta-annotated type is used as a member type in 
  10.  * another annotation type. 
  11.  * 
  12.  * @author  Joshua Bloch 
  13.  * @since 1.5 
  14.  * @jls 9.6.3.2 @Retention 
  15.  */ 
  16. @Documented 
  17. @Retention(RetentionPolicy.RUNTIME) 
  18. @Target(ElementType.ANNOTATION_TYPE) 
  19. public @interface Retention { 
  20.     /** 
  21.      * Returns the retention policy. 
  22.      * @return the retention policy 
  23.      */ 
  24.     RetentionPolicy value(); 

moon:再次通過我強大的英文閱讀能力看下,這個注釋到底是什么意思?

圖片

moon:其實它就是告訴你,該注解的「生命周期」有多久,而這個生命周期的定義,「就在 RetentionPolicy 里面」,我們再來看看這個 RetentionPolicy 到底是什么?

  1. public enum RetentionPolicy { 
  2.     /** 
  3.      * Annotations are to be discarded by the compiler.關注公眾號:moon聊技術,獲取更多有趣文章 
  4.      */ 
  5.     SOURCE, 
  6.  
  7.     /** 
  8.      * Annotations are to be recorded in the class file by the compiler 
  9.      * but need not be retained by the VM at run time.  This is the default 
  10.      * behavior. 
  11.      */ 
  12.     CLASS, 
  13.  
  14.     /** 
  15.      * Annotations are to be recorded in the class file by the compiler and 
  16.      * retained by the VM at run time, so they may be read reflectively. 
  17.      * 
  18.      * @see java.lang.reflect.AnnotatedElement 
  19.      */ 
  20.     RUNTIME 

女朋友:這個我熟!「SOURCE 的意思就是說被作用在源代碼上,CLASS 就是被作用在編譯出來的源碼上,RUNTIME 就是只作用在運行時」!這不就是 Java 的三種狀態嘛!

moon:你都學會搶答了我的寶!!!!

女朋友:哼!快繼續!!

moon:哈哈哈,好的,那我們就來說說最后一個注解 「@Target」

  1. @Documented 
  2. @Retention(RetentionPolicy.RUNTIME) 
  3. @Target(ElementType.ANNOTATION_TYPE) 
  4. public @interface Target { 
  5.     /** 
  6.      * Returns an array of the kinds of elements an annotation type 
  7.      * can be applied to
  8.      * @return an array of the kinds of elements an annotation type 
  9.      * can be applied to 
  10.      */ 
  11.     ElementType[] value(); 

moon:這個注解的作用其實很簡單,「就是告訴你該注解可以被貼在哪些作用域中」,而作用域有哪些你知道嗎?

女朋友:嗯...有類、方法、成員變量.....

moon:哈哈哈哈哈,不知道了吧!!

女朋友:哼!!「分手」!!!!

moon:別別別別別別,聽我給你娓娓道來!這個作用域其實就藏在 「ElementType[]」 這個數組當中,我們進去看下!

  1. public enum ElementType { 
  2.     /** Class, interface (including annotation type), or enum declaration 關注公眾號:moon聊技術,獲取更多有趣文章*/ 
  3.     TYPE, 
  4.  
  5.     /** Field declaration (includes enum constants) */ 
  6.     FIELD, 
  7.  
  8.     /** Method declaration */ 
  9.     METHOD, 
  10.  
  11.     /** Formal parameter declaration */ 
  12.     PARAMETER, 
  13.  
  14.     /** Constructor declaration */ 
  15.     CONSTRUCTOR, 
  16.  
  17.     /** Local variable declaration */ 
  18.     LOCAL_VARIABLE, 
  19.  
  20.     /** Annotation type declaration */ 
  21.     ANNOTATION_TYPE, 
  22.  
  23.     /** Package declaration */ 
  24.     PACKAGE, 
  25.  
  26.     /** 
  27.      * Type parameter declaration 
  28.      * 
  29.      * @since 1.8 
  30.      */ 
  31.     TYPE_PARAMETER, 
  32.  
  33.     /** 
  34.      * Use of a type 
  35.      * 
  36.      * @since 1.8 
  37.      */ 
  38.     TYPE_USE 

moon:總共有「10種作用域」

所以當你確定你注解的作用域之后,你貼上 @Target(作用域),就可以了!

女朋友:噢噢噢噢,我懂了,那我有個問題,「如果我想讓我的子類也繼承這個注解該怎么做呢」?

moon:!!!!!!!這就是我接下來要講的!!「@Inherited」 !!也是 java 四大元注解之一(還有三個就是剛剛提到的@Target,@Retention,@Documented)!它的作用就是「讓子類也能繼承該父類的該注解」,那你知道該怎么用嘛?

女朋友:分....

moon:我來給你舉個例子!正好練習一下!

女朋友:哼!

moon:我們先寫個注解類

  1. @Retention(RetentionPolicy.RUNTIME) 
  2. @Target(ElementType.METHOD) 
  3. public @interface MyAnnotation { 
  4.     /** 
  5.      * 說我愛你(默認true
  6.      */ 
  7.     boolean sayILoveYou() default true

moon:這個注解很簡單,「只能作用在方法上,在運行時實現,有個 syaILoveYou 的方法,默認是true!」

女朋友:yue~快說

moon:哈哈,再定義一個我,有個 sayLoveYou()方法,貼上了我們的 @MyAnnotation 注解,表達一下我的真心

  1. public class Me { 
  2.     @MyAnnotation 
  3.     public void sayLoveYou(){ 
  4.         System.out.println("表達一下我的真心"); 
  5.     } 

女朋友:yue~

moon:好了,現在我們開始測試了!

  1. public class Main { 
  2.     public static void main(String[] args) { 
  3.         try { 
  4.             //獲取Me的Class對象 
  5.             Me me = new Me(); 
  6.             Class clazz = me.getClass(); 
  7.             //獲取該對象sayLoveYou方法上Info類型的注解 
  8.             MyAnnotation myAnnotation = clazz.getMethod("sayLoveYou"null).getDeclaredAnnotation(MyAnnotation.class); 
  9.             if (myAnnotation.sayILoveYou()) { 
  10.                 System.out.println("我愛你"); 
  11.             } else { 
  12.                 System.out.println("我不愛你"); 
  13.             } 
  14.         } catch (Exception e) { 
  15.             e.printStackTrace(); 
  16.         } 
  17.     } 

moon:我們先獲取到了 Me 的對象,然后獲取到了 MyAnnotation 這個注解,如果 myAnnotation.sayILoveYou() 為true,就會輸出"我愛你"!如果為false,就會輸出"我不愛你"!

女朋友:你不愛我,「我們分手」

moon:咳咳,測試測試~我們運行看下,結果一定是我愛你!因為我們默認為true

圖片

moon:我們修改下注解的默認值,結果就為我EN愛你了(滿滿的求生欲)

  1. public class Me { 
  2.     @MyAnnotation(sayILoveYou=false
  3.     public void sayLoveYou(){ 
  4.         System.out.println("表達一下我的真心"); 
  5.     } 
圖片

女朋友:哼~

moon:我們再試驗下 @Inherited 這個注解,修改下 MyAnnotation,「添加 @Inherited」,添「加 ElementType.TYPE 并且使其可以作用在類上」

  1. @Retention(RetentionPolicy.RUNTIME) 
  2. @Target({Ele,mentType.METHOD,ElementType.TYPE}) 
  3. @Inherited 
  4. public @interface MyAnnotation { 
  5.     /** 
  6.      * 說我愛你(默認true
  7.      */ 
  8.     boolean sayILoveYou() default true

moon:Me 這個類在類上貼 @MyAnnotation 注解

  1. @MyAnnotation 
  2. public class Me { 
  3.     public void sayLoveYou(){ 
  4.         System.out.println("表達一下我的真心"); 
  5.     } 

moon:然后我們假如有孩子了

  1. public class Child extends Me{ 

女朋友:我不會和你結婚的!

moon:哈哈哈,假設假設,我們再來重寫 Main 方法

  1. public static void main(String[] args) { 
  2.    try { 
  3.        //獲取child的Class對象 
  4.        Child child = new Child(); 
  5.        Class clazz = child.getClass(); 
  6.        //獲取該對象sayLoveYou方法上Info類型的注解 
  7.        MyAnnotation myAnnotation = (MyAnnotation) clazz.getAnnotation(MyAnnotation.class); 
  8.        if (myAnnotation.sayILoveYou()) { 
  9.            System.out.println("我愛你"); 
  10.        } else { 
  11.            System.out.println("我不愛你"); 
  12.        } 
  13.    } catch (Exception e) { 
  14.        e.printStackTrace(); 
  15.    } 

moon:「我們此時 child 對象是沒有 @MyAnnotation 注解的,只是繼承了我,但是由于我們再 Me 類貼了 @MyAnnotation 注解,并且有 @Inherited 注解,所以 child 也有該注解的功能,所以運行結果一定是我愛你!」

圖片

moon:這下你會了吧!注解就是這么簡單!

女朋友:哼,你還是有點用的,我不需要你了,你走吧

moon:好的老板!(終于教會了,我又活下來了)

一共分了多少次手,你們數清楚了嗎?

 

責任編輯:姜華 來源: moon聊技術
相關推薦

2019-04-16 14:31:21

華為離職移動

2021-03-03 09:16:51

容器技術容器云計算

2018-04-24 18:23:02

數據庫誤刪

2020-11-08 14:34:31

小視頻瀏覽器

2024-03-28 09:24:31

AI語言模型技術

2019-07-22 09:55:43

誤刪數據庫用戶庫

2020-01-02 09:14:23

Kubernetes內部容器

2019-10-24 09:29:04

程序員程序員節女朋友

2019-08-28 16:22:30

Python數據微信

2019-11-19 11:29:50

Python數據標系

2015-08-26 10:17:29

程序員女朋友

2023-04-12 08:45:21

ChatGPTPrompt技巧

2021-02-02 11:59:15

插件開發工具

2021-02-20 07:52:35

防猝死插件 IDEA

2020-04-21 11:45:04

技巧單一責任鏈開閉原則

2022-09-17 08:10:20

HSV飽和度圖像

2013-01-04 10:20:27

互聯網產品

2020-09-02 08:52:16

地圖Echarts可視化

2019-07-01 09:31:04

拉黑復活檢測器

2019-07-09 09:19:51

分布式事務App
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 7777奇米影视 | 国产小视频自拍 | 精品国产一区久久 | 欧美日韩久 | 久久久久久免费免费 | 日韩精品免费播放 | 精品三级在线观看 | 日韩中文在线视频 | 久久精品亚洲精品国产欧美 | 91久久精品国产 | 日韩视频一区二区三区 | 欧美黄在线观看 | 盗摄精品av一区二区三区 | 免费黄色大片 | 国产高清在线观看 | 最新中文字幕第一页视频 | 精品久久久久一区二区国产 | 精品成人一区二区 | 一区免费 | 亚洲第一黄色网 | 日韩欧美在线免费观看 | 毛片日韩 | 精品久久久久久久 | 国产欧美日韩综合精品一 | 日韩精品成人网 | 秋霞在线一区二区 | 国产精品视频免费观看 | 精品久久不卡 | 成人国内精品久久久久一区 | 久久成人人人人精品欧 | 欧美日韩在线一区二区 | 欧美一级二级视频 | 成人欧美一区二区三区在线播放 | 国产成人高清 | 久久伊人精品一区二区三区 | 亚洲www啪成人一区二区 | 中文字幕免费中文 | 天天爽夜夜爽精品视频婷婷 | 天天操天天干天天曰 | 国产成人在线一区二区 | 色999视频 |