{"id":1175,"date":"2022-10-25T22:51:31","date_gmt":"2022-10-25T14:51:31","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1175"},"modified":"2022-11-27T12:39:49","modified_gmt":"2022-11-27T04:39:49","slug":"%e6%9e%9a%e4%b8%be%e7%b1%bb%e4%b8%8e%e6%b3%a8%e8%a7%a3-annotation%e7%9a%84%e4%bd%bf%e7%94%a8%e7%a4%ba%e4%be%8b%e3%80%81jdk%e5%86%85%e7%bd%ae%e7%9a%84%e4%b8%89%e4%b8%aa%e5%9f%ba%e6%9c%ac%e6%b3%a8","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1175","title":{"rendered":"\u679a\u4e3e\u7c7b\u4e0e\u6ce8\u89e3-Annotation\u7684\u4f7f\u7528\u793a\u4f8b\u3001JDK\u5185\u7f6e\u7684\u4e09\u4e2a\u57fa\u672c\u6ce8\u89e3\u3001\u5982\u4f55\u81ea\u5b9a\u4e49\u6ce8\u89e3\u3001jdk\u4e2d4\u4e2a\u57fa\u672c\u7684\u5143\u6ce8\u89e3\u7684\u4f7f\u75281\u3001jdk\u4e2d4\u4e2a\u57fa\u672c\u7684\u5143\u6ce8\u89e3\u7684\u4f7f\u75282\u3001jdk8\u65b0\u7279\u6027\uff1a\u53ef\u91cd\u590d\u6ce8\u89e3\u3001jdk8\u65b0\u7279\u6027\uff1a\u7c7b\u578b\u6ce8\u89e3\u3001\u53ef\u91cd\u590d\u6ce8\u89e3\u4e2d\u5143\u6ce8\u89e3\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u89e3\u51b3"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P505;\r\n\r\nimport org.junit.jupiter.api.Test;\r\n\r\nimport java.lang.annotation.Annotation;\r\nimport java.util.ArrayList;\r\nimport java.util.Date;\r\n\r\n\/**\r\n * \u6ce8\u89e3\u7684\u4f7f\u7528\r\n *\r\n * 1. \u7406\u89e3Annotation:\r\n * \u2460 jdk 5.0 \u65b0\u589e\u7684\u529f\u80fd\r\n * \u2461 Annotation \u5176\u5b9e\u5c31\u662f\u4ee3\u7801\u91cc\u7684\u7279\u6b8a\u6807\u8bb0, \u8fd9\u4e9b\u6807\u8bb0\u53ef\u4ee5\u5728\u7f16\u8bd1, \u7c7b\u52a0\u8f7d, \u8fd0\u884c\u65f6\u88ab\u8bfb\u53d6, \u5e76\u6267\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002\u901a\u8fc7\u4f7f\u7528 Annotation,\r\n * \u7a0b\u5e8f\u5458\u53ef\u4ee5\u5728\u4e0d\u6539\u53d8\u539f\u6709\u903b\u8f91\u7684\u60c5\u51b5\u4e0b, \u5728\u6e90\u6587\u4ef6\u4e2d\u5d4c\u5165\u4e00\u4e9b\u8865\u5145\u4fe1\u606f\u3002\r\n *\r\n * \u2462 \u5728JavaSE\u4e2d\uff0c\u6ce8\u89e3\u7684\u4f7f\u7528\u76ee\u7684\u6bd4\u8f83\u7b80\u5355\uff0c\u4f8b\u5982\u6807\u8bb0\u8fc7\u65f6\u7684\u529f\u80fd\uff0c\u5ffd\u7565\u8b66\u544a\u7b49\u3002\u5728JavaEE\/Android\r\n * \u4e2d\u6ce8\u89e3\u5360\u636e\u4e86\u66f4\u91cd\u8981\u7684\u89d2\u8272\uff0c\u4f8b\u5982\u7528\u6765\u914d\u7f6e\u5e94\u7528\u7a0b\u5e8f\u7684\u4efb\u4f55\u5207\u9762\uff0c\u4ee3\u66ffJavaEE\u65e7\u7248\u4e2d\u6240\u9057\u7559\u7684\u7e41\u5197\r\n * \u4ee3\u7801\u548cXML\u914d\u7f6e\u7b49\u3002\r\n *\r\n * 2. Annocation\u7684\u4f7f\u7528\u793a\u4f8b\r\n * \u793a\u4f8b\u4e00\uff1a\u751f\u6210\u6587\u6863\u76f8\u5173\u7684\u6ce8\u89e3\r\n *\r\n * \u793a\u4f8b\u4e8c\uff1a\u5728\u7f16\u8bd1\u65f6\u8fdb\u884c\u683c\u5f0f\u68c0\u67e5(JDK\u5185\u7f6e\u7684\u4e09\u4e2a\u57fa\u672c\u6ce8\u89e3)\r\n *  \uf0d8@Override: \u9650\u5b9a\u91cd\u5199\u7236\u7c7b\u65b9\u6cd5, \u8be5\u6ce8\u89e3\u53ea\u80fd\u7528\u4e8e\u65b9\u6cd5\r\n *  \uf0d8@Deprecated: \u7528\u4e8e\u8868\u793a\u6240\u4fee\u9970\u7684\u5143\u7d20(\u7c7b, \u65b9\u6cd5\u7b49)\u5df2\u8fc7\u65f6\u3002\u901a\u5e38\u662f\u56e0\u4e3a\u6240\u4fee\u9970\u7684\u7ed3\u6784\u5371\u9669\u6216\u5b58\u5728\u66f4\u597d\u7684\u9009\u62e9\r\n *  \uf0d8@SuppressWarnings: \u6291\u5236\u7f16\u8bd1\u5668\u8b66\u544a\r\n *\r\n * \u793a\u4f8b\u4e09\uff1a\u8ddf\u8e2a\u4ee3\u7801\u4f9d\u8d56\u6027\uff0c\u5b9e\u73b0\u66ff\u4ee3\u914d\u7f6e\u6587\u4ef6\u529f\u80fd\r\n *\r\n * 3. \u5982\u4f55\u81ea\u5b9a\u4e49\u6ce8\u89e3\uff1a\u53c2\u7167@SuppressWarnings\u5b9a\u4e49\r\n *      \u2460 \u6ce8\u89e3\u58f0\u660e\u4e3a@interface\r\n *      \u2461 \u5185\u90e8\u5b9a\u4e49\u6210\u5458\uff0c\u901a\u5e38\u4f7f\u7528value\u8868\u793a\r\n *      \u2462 \u53ef\u4ee5\u6307\u5b9a\u6210\u5458\u7684\u9ed8\u8ba4\u503c\uff0c\u4f7f\u7528default\u5b9a\u4e49\r\n *      \u2463 \u5982\u679c\u81ea\u5b9a\u4e49\u6ce8\u89e3\u6ca1\u6709\u6210\u5458\uff0c\u8868\u660e\u662f\u4e00\u4e2a\u6807\u8bc6\u4f5c\u7528\u3002\r\n *\r\n *      \u5982\u679c\u6ce8\u89e3\u6709\u6210\u5458\uff0c\u5728\u4f7f\u7528\u6ce8\u89e3\u65f6\uff0c\u9700\u8981\u6307\u660e\u6210\u5458\u7684\u503c\u3002\r\n *      \u81ea\u5b9a\u4e49\u6ce8\u89e3\u5fc5\u987b\u914d\u4e0a\u6ce8\u89e3\u7684\u4fe1\u606f\u5904\u7406\u6d41\u7a0b\uff08\u4f7f\u7528\u53cd\u5c04\uff09\u624d\u6709\u610f\u4e49\u3002\r\n *      \u81ea\u5b9a\u4e49\u6ce8\u89e3\u901a\u5e38\u90fd\u4f1a\u6307\u660e\u4e24\u4e2a\u5143\u6ce8\u89e3\uff1aRetention\u3001Target\r\n *\r\n * 4. jdk \u63d0\u4f9b\u76844\u79cd\u5143\u6ce8\u89e3\r\n *    \u5143\u6ce8\u89e3\uff1a\u5bf9\u73b0\u6709\u7684\u6ce8\u89e3\u8fdb\u884c\u89e3\u91ca\u8bf4\u660e\u7684\u6ce8\u89e3\r\n *      \uf0d8Retention\uff1a\u6307\u5b9a\u6240\u4fee\u9970\u7684Annotation \u7684\u751f\u547d\u5468\u671f\uff1aSOURCE\\CLASS(\u9ed8\u8ba4\u884c\u4e3a)\\RUNTIME\r\n *              \u53ea\u6709\u58f0\u660e\u4e3aRUNTIME\u751f\u547d\u5468\u671f\u7684\u6ce8\u89e3\uff0c\u624d\u80fd\u901a\u8fc7\u53cd\u5c04\u83b7\u53d6\u3002\r\n *      \uf0d8Target\uff1a\u7528\u4e8e\u6307\u5b9a\u88ab\u4fee\u9970\u7684 Annotation \u80fd\u7528\u4e8e\u4fee\u9970\u54ea\u4e9b\u7a0b\u5e8f\u5143\u7d20\r\n *      ********\u51fa\u73b0\u7684\u9891\u7387\u8f83\u4f4e***************\r\n *      \uf0d8Documented\uff1a\u8868\u793a\u6240\u4fee\u9970\u7684\u6ce8\u89e3\u5728\u88abjavadoc\u89e3\u6790\u65f6\uff0c\u4fdd\u7559\u4e0b\u6765\u3002\r\n *      \uf0d8Inherited\uff1a\u88ab\u5b83\u4fee\u9970\u7684 Annotation \u5c06\u5177\u6709\u7ee7\u627f\u6027\u3002\r\n *\r\n * 5. \u901a\u8fc7\u53cd\u5c04\u83b7\u53d6\u6ce8\u89e3\u4fe1\u606f ---\u5230\u53cd\u5c04\u5185\u5bb9\u65f6\u7cfb\u7edf\u8bb2\u89e3\r\n *\r\n * 6. jdk 8 \u79cd\u6ce8\u89e3\u7684\u65b0\u7279\u6027\uff1a\u53ef\u91cd\u590d\u6ce8\u89e3\u3001\u7c7b\u578b\u6ce8\u89e3\r\n * 6.1 \u53ef\u91cd\u590d\u6ce8\u89e3\uff1a\u2460 \u5728MyAnnotation\u4e0a\u58f0\u660e@Repeatable\uff0c\u6210\u5458\u503c\u4e3aMyAnnotations.class\r\n *                 \u2461 MyAnnotation\u7684Target\u548cRetention\u7b49\u5143\u6ce8\u89e3\u4e0eMyAnnotations\u76f8\u540c\u3002\r\n *\r\n * 6.2 \u7c7b\u578b\u6ce8\u89e3\uff1a\r\n * \uf0d8 ElementType.TYPE_PARAMETER \u8868\u793a\u8be5\u6ce8\u89e3\u80fd\u5199\u5728\u7c7b\u578b\u53d8\u91cf\u7684\u58f0\u660e\u8bed\u53e5\u4e2d\uff08\u5982\uff1a\u6cdb\u578b\u58f0\u660e\uff09\u3002\r\n * \uf0d8 ElementType.TYPE_USE \u8868\u793a\u8be5\u6ce8\u89e3\u80fd\u5199\u5728\u4f7f\u7528\u7c7b\u578b\u7684\u4efb\u4f55\u8bed\u53e5\u4e2d\u3002\r\n *\r\n *\r\n * @author RichieZhang\r\n * @create 2022-10-25 \u4e0b\u5348 2:34\r\n *\/\r\npublic class AnnotationTest {\r\n\r\n    public static void main(String&#91;] args) {\r\n\r\n        Person p = new Student();\r\n        p.walk();\r\n\r\n        Date date = new Date(2020, 10, 11);\r\n        System.out.println(date);\r\n\r\n        @SuppressWarnings(\"unused\")\r\n        int num = 10;\r\n\/\/        System.out.println(num);\r\n\r\n        @SuppressWarnings({ \"unused\" , \"rawtypes\" })\r\n        ArrayList list = new ArrayList();\r\n    }\r\n\r\n    @Test\r\n    public void testGetAnnotation(){\r\n        Class clazz = Student.class;\r\n        Annotation&#91;] annotations = clazz.getAnnotations();\r\n        for (int i = 0; i &lt; annotations.length; i++) {\r\n            System.out.println(annotations&#91;i]);\r\n        }\r\n    }\r\n}\r\n\r\n\/\/jdk 8\u4e4b\u524d\u7684\u5199\u6cd5\uff1a\r\n\/\/@MyAnnotations({@MyAnnotation(value = \"hi\"),@MyAnnotation(value = \"abc\")})\r\n@MyAnnotation(value = \"hi\")\r\n@MyAnnotation(value = \"abc\")\r\n\r\nclass Person{\r\n    private String name;\r\n    private int age;\r\n\r\n    public Person() {\r\n    }\r\n\r\n    @MyAnnotation\r\n    public Person(String name, int age) {\r\n        this.name = name;\r\n        this.age = age;\r\n    }\r\n\r\n    @MyAnnotation\r\n    public void walk(){\r\n        System.out.println(\"\u4eba\u8d70\u8def\");\r\n    }\r\n\r\n    public void eat(){\r\n        System.out.println(\"\u4eba\u5403\u996d\");\r\n    }\r\n}\r\n\r\ninterface Info{\r\n    void show();\r\n}\r\n\r\nclass Student extends Person implements Info{\r\n\r\n    @Override\r\n    public void walk() {\r\n        System.out.println(\"\u5b66\u751f\u8d70\u8def\");\r\n    }\r\n\r\n\r\n    public void show() {\r\n\r\n    }\r\n}\r\n\r\nclass Generic&lt;@MyAnnotation T>{\r\n\r\n    public void show() throws @MyAnnotation RuntimeException{\r\n\r\n        ArrayList&lt;@MyAnnotation String> list = new ArrayList&lt;>();\r\n\r\n        int num = (@MyAnnotation int)10L;\r\n\r\n\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P505;\r\n\r\nimport java.lang.annotation.*;\r\n\r\nimport static java.lang.annotation.ElementType.*;\r\n\r\n\/**\r\n * @author RichieZhang\r\n * @create 2022-10-25 \u4e0b\u5348 3:03\r\n *\/\r\n@Inherited\r\n@Repeatable(MyAnnotations.class)\r\n@Retention(RetentionPolicy.RUNTIME)\r\n@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, MODULE,TYPE_PARAMETER,TYPE_USE})\r\npublic @interface MyAnnotation {\r\n\r\n    String value() default \"hello\";\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P505;\r\n\r\nimport java.lang.annotation.Inherited;\r\nimport java.lang.annotation.Retention;\r\nimport java.lang.annotation.RetentionPolicy;\r\nimport java.lang.annotation.Target;\r\n\r\nimport static java.lang.annotation.ElementType.*;\r\n\r\n\/**\r\n * @author RichieZhang\r\n * @create 2022-10-25 \u4e0b\u5348 5:57\r\n *\/\r\n@Inherited\r\n@Retention(RetentionPolicy.RUNTIME)\r\n@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, MODULE})\r\npublic @interface MyAnnotations {\r\n\r\n    MyAnnotation&#91;] value();\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-1175","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1175"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1175"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1175\/revisions"}],"predecessor-version":[{"id":1176,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1175\/revisions\/1176"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}