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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JDK注解(内置和自定义)

發布時間:2025/5/22 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JDK注解(内置和自定义) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JDK注解(內置和自定義)

1.內置

  • @Override:可以確保重寫的方法,的確存在與父類、接口中,可以有效避免單詞拼錯的情況
  • @Deprecated:給用于提示,該方法由于安全,性能問題等,已經不推薦使用。此外在版本升級時,如果要計劃刪除一些方法,也通常會在前一個版本中加上@Deprecated,然后再在后續版本中刪除
    3.@SuppressWarnings(value = “unchecked”)
    //壓制警告(雖然可以使用SuppressWarning壓制警告,但不建議使用。)//忽略對泛型等的檢查操作。
    其中的一些屬性和值:
    • value值:unchecked,deprecation(忽略一些過期的API),

    • unused(是否未被使用),

    • fallthrough(switch是否一致往下執行,而沒有break),

    • path(忽略對類路徑不存在的檢查),

    • serialversionUID(忽略一個類可以序列化,但卻沒有序列化的警告),

    • all,全部忽略

    2.自定義
    引入元注解:即修飾注解的注解
    常用的幾個元注解:

    (1)@Target:限制注解,使用位置(ElementType.枚舉)
    限制注解能夠使用在屬性、方法、類中。如果一個注解沒有@Target描述,則可以修飾任何類型的元素;如果有@Target修飾,該注解只能用于被@Target修飾的地方
    下圖見源碼:

    @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Target {/*** Returns an array of the kinds of elements an annotation type* can be applied to.* @return an array of the kinds of elements an annotation type* can be applied to*/ElementType[] value(); }

    上圖可知:使用時需要用ElementType來進行約束
    ElementType:

    public enum ElementType {/** Class, interface (including annotation type), or enum declaration */TYPE,/** Field declaration (includes enum constants) */FIELD,/** Method declaration */METHOD,/** Formal parameter declaration */PARAMETER,/** Constructor declaration */CONSTRUCTOR,/** Local variable declaration */LOCAL_VARIABLE,/** Annotation type declaration */ANNOTATION_TYPE,/** Package declaration */PACKAGE,/*** Type parameter declaration** @since 1.8*/TYPE_PARAMETER,/*** Use of a type** @since 1.8*/TYPE_USE }

    ElementType就是用來約束使用的地方,和時間

    @Rentention源碼:

    @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention {/*** Returns the retention policy.* @return the retention policy*/RetentionPolicy value(); }

    @Document
    javadoc:java幫助文檔。ABC.java->幫助文檔
    默認情況下,Javadoc不包含注解的解釋,如果現在javadoc文檔中也包含對注解的說明,則需要使用@Document標注

    Document源碼:

    package java.lang.annotation;/*** Indicates that annotations with a type are to be documented by javadoc* and similar tools by default. This type should be used to annotate the* declarations of types whose annotations affect the use of annotated* elements by their clients. If a type declaration is annotated with* Documented, its annotations become part of the public API* of the annotated elements.** @author Joshua Bloch* @since 1.5*/ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Documented { }

    @Inherited:繼承

    源碼:

    package java.lang.annotation;/*** Indicates that an annotation type is automatically inherited. If* an Inherited meta-annotation is present on an annotation type* declaration, and the user queries the annotation type on a class* declaration, and the class declaration has no annotation for this type,* then the class's superclass will automatically be queried for the* annotation type. This process will be repeated until an annotation for this* type is found, or the top of the class hierarchy (Object)* is reached. If no superclass has an annotation for this type, then* the query will indicate that the class in question has no such annotation.** <p>Note that this meta-annotation type has no effect if the annotated* type is used to annotate anything other than a class. Note also* that this meta-annotation only causes annotations to be inherited* from superclasses; annotations on implemented interfaces have no* effect.** @author Joshua Bloch* @since 1.5* @jls 9.6.3.3 @Inherited*/ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Inherited { }

    使用實例:

    package Annotation;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;@Target(value = {ElementType.METHOD,ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation {String value() default "張三";int age() default 24; } package Annotation;import java.lang.annotation.Annotation;public class Test {@MyAnnotation(value = "ls",age = 21)@Deprecated //不推薦使用注解public static void test() throws Exception {Annotation[] declaredAnnotations = Class.forName("Annotation.Test").getMethod("test").getDeclaredAnnotations();for(Annotation d:declaredAnnotations){if(d instanceof MyAnnotation){System.out.println(((MyAnnotation) d).age());System.out.println(((MyAnnotation) d).value());}else{System.out.println("Deprecated"); //不符合條件不推薦}}}public static void main(String[] args) throws Exception {test();} }

    結果:

    修改代碼:修改生命周期,將RetentionPolicy.RUNTIME修改為下圖:

    結果變為:

    其他的幾個元注解調試也類似。

    總結

    以上是生活随笔為你收集整理的JDK注解(内置和自定义)的全部內容,希望文章能夠幫你解決所遇到的問題。

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

    主站蜘蛛池模板: 可以免费观看的av网站 | 中文字幕 国产 | 亚洲av永久无码国产精品久久 | 涩涩涩涩av | 91香蕉视频黄色 | 亚洲无限观看 | 日韩午夜视频在线 | 日韩毛片中文字幕 | 亚洲综合一区在线观看 | 秋霞欧美一区二区三区视频免费 | 7777av| 欧美日韩色视频 | 欧美日韩一二 | 日韩毛片免费观看 | 在线观看视频国产 | 狠狠干女人| 亚洲一区二区视频网站 | 亚洲色图导航 | 日韩国产电影 | 欧美黄色成人 | 国产资源在线视频 | 色狠狠久久av大岛优香 | 芒果视频污污 | 最新av中文字幕 | 懂色av中文一区二区三区天美 | 欧美性福利| 黄视频网站在线 | 国产chinese中国hdxxxx | 成年人视频在线免费观看 | 小宝贝真紧h军人h | 91精品久久久久久久久久久 | 成人1区 | 五月婷婷亚洲 | 国产三级视频在线播放 | 日韩污视频 | 凸凹人妻人人澡人人添 | 秋霞毛片少妇激情免费 | 国产 一二三四五六 | 中文字幕在线观看第二页 | 来吧亚洲综合网 | 狠狠cao日日穞夜夜穞av | 91视频青青草 | 亚洲一卡二卡三卡四卡 | 国产无毛片 | 国产不卡视频在线 | 风流少妇 | 激情欧美一区二区 | 欧美丰满美乳xxx高潮www | 国产妇女馒头高清泬20p多 | 韩日精品在线观看 | 911美女片黄在线观看游戏 | 亚洲欧洲av在线 | 久久男 | 久久免费精品国产 | 免费的黄色的视频 | 91麻豆精品91久久久久同性 | 亚洲一级无毛 | 国产极品999 | 久久久久亚洲av无码专区 | 欧美激情aaa | 高清欧美性猛交 | 95国产精品 | 性视频在线| 日韩毛片网 | 欧美日韩中文一区 | 波多野结衣在线播放视频 | 日韩精品福利 | 日本免费一区二区在线 | xx性欧美肥妇精品久久久久久 | 亚欧在线视频 | 青青青网| 亚洲天堂中文字幕在线观看 | 久久麻豆视频 | 男女做爰猛烈刺激 | www.国产.com | 亚洲天堂一区二区在线 | 波多野结衣激情视频 | 九色在线观看 | 成人精品视频99在线观看免费 | 波多野结衣视频免费在线观看 | 在线观看国产麻豆 | 黄色在线视频网站 | 性激情视频 | 天天干天天天天 | 国产伦精品一区二区三区照片91 | 日韩在线视频网址 | 最新日本中文字幕 | 亚洲人人夜夜澡人人爽 | 亚洲精品一区二区三区精华液 | 亚洲最大的黄色网 | 免费伊人网 | 伊人99热 | 欧美在线a| www,久久久 | 久久人人爽天天玩人人妻精品 | 午夜欧美在线 | 6080电视影片在线观看 | 韩国电影一区二区三区 | 久久人妻少妇嫩草av |