日韩性视频-久久久蜜桃-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免费一区 | 亚洲人妖在线 | 台湾极品xxx少妇 | 免费黄色欧美 | 白石茉莉奈中文字幕在 | 国产麻豆剧果冻传媒白晶晶 | 久久久久久av无码免费网站 | 一级黄色片在线播放 | 欧美另类人妖 | 欧美亚洲另类小说 | 九九热视频精品 | 女人扒开腿免费视频app | 中文字幕一区二区三区人妻 | 久久久久亚洲av成人片 | 日韩在线资源 | 黄色无遮挡 | 久久靖品| 9999av| 成年人免费在线观看视频网站 | 色婷婷av一区二区三区之e本道 | 亚洲av成人无码久久精品老人 | 欧美日韩国产高清 | 婷婷影音 | 亚洲视频 欧美视频 | 久久天天| 日本孰妇毛茸茸xxxx | 菲律宾av | 青草精品在线 | 日本青青草| 日韩久久中文字幕 | 国产精品无码免费专区午夜 | 中文字幕在线观看视频www | 男生裸体视频网站 | 亚洲白浆| 91中文字幕在线视频 | 亚洲精品久久久久久宅男 | 国产精品久久久久久人妻精品动漫 | 麻豆传谋在线观看免费mv | 色婷婷一区二区 | 日韩精品免费一区二区在线观看 | 欧美精品h| 快播色图 | 亚洲乱码国产一区三区 | 少妇高潮一区二区三区69 | 免费欧美日韩 | 亚洲精品污一区二区三区 | 四虎影院黄色 | 精品美女久久 | 狠狠撸视频 | 少妇脱了内裤让我添 | 91中文字幕在线观看 | 色哟哟视频网站 | 国产天堂网 | 天天插天天干 | 亚洲a网站 | 一区二区三区亚洲精品 | 精品少妇人妻av免费久久洗澡 | 久久av免费看| 婷综合 | 日本在线二区 | 97超碰人人澡人人爱学生 | 日日干夜夜爱 | 住在隔壁的她动漫免费观看全集下载 | 久草中文在线 | 人妖粗暴刺激videos呻吟 | 淫五月| 自拍亚洲一区 | 午夜资源 | 91蜜桃视频| 欧美熟妇乱码在线一区 | 国产主播一区二区 | 喷水视频在线观看 | 91淫黄大片| 亚洲欧美另类在线视频 | 最新中文在线视频 | 亚洲视频一区在线播放 | 日本电影一区 | 色中色在线视频 | 久久久久精彩视频 | www污污 | 姑娘第5集在线观看免费 | 精品国产乱码久久久久久蜜臀网站 | 青青青草视频在线 | 黄色小视频免费在线观看 | 欧美,日韩,国产精品免费观看 | 国产亚洲精品久久久久婷婷瑜伽 | 玖玖色资源 | 一卡二卡在线视频 | 国产成人精品一区二区无码呦 | www日韩av| 快播久久 | 17c精品麻豆一区二区免费 | 国产99精品视频 | 少妇视频一区二区三区 | 小镇姑娘国语版在线观看免费 |