{"id":1442,"date":"2022-03-28T11:51:06","date_gmt":"2022-03-28T03:51:06","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=1442"},"modified":"2022-03-28T12:20:58","modified_gmt":"2022-03-28T04:20:58","slug":"%e6%b3%a8%e8%a7%a3","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2022\/03\/28\/%e6%b3%a8%e8%a7%a3\/","title":{"rendered":"\u6ce8\u89e3"},"content":{"rendered":"<p>\u6ce8\u89e3\u672c\u8d28\u4e0a\u4e5f\u662f\u4e00\u4e2a\u7c7b\uff0c\u4f46\u7528\u6cd5\u6bd4\u8f83\u7279\u6b8a\u3002\u6ce8\u89e3\u53ef\u4ee5\u88ab\u6807\u6ce8\u5728\u4efb\u610f\u5730\u65b9\uff0c\u5305\u62ec\u65b9\u6cd5\u4e0a\u3001\u7c7b\u540d\u4e0a\u3001\u53c2\u6570\u4e0a\u3001\u6210\u5458\u5c5e\u6027\u4e0a\u3001\u6ce8\u89e3\u5b9a\u4e49\u4e0a\u7b49\uff0c\u5c31\u50cf\u6ce8\u91ca\u4e00\u6837\uff0c\u5b83\u76f8\u5f53\u4e8e\u6211\u4eec\u5bf9\u67d0\u6837\u4e1c\u897f\u7684\u4e00\u4e2a\u6807\u8bb0\u3002\u800c\u4e0e\u6ce8\u91ca\u4e0d\u540c\u7684\u662f\uff0c\u6ce8\u89e3\u53ef\u4ee5\u901a\u8fc7\u53cd\u5c04\u5728\u8fd0\u884c\u65f6\u83b7\u53d6\uff0c\u6ce8\u89e3\u4e5f\u53ef\u4ee5\u9009\u62e9\u662f\u5426\u4fdd\u7559\u5230\u8fd0\u884c\u65f6\u3002<\/p>\n<p>\u58f0\u660e\u4e00\u4e2a\u6ce8\u89e3\u7c7b\u578b\uff1a<\/p>\n<pre><code class=\"language-java\">@Target({ElementType.METHOD, ElementType.TYPE})\n@Retention(RetentionPolicy.RUNTIME)\npublic @interface Test{\n    String value() default &quot;\u8fd9\u662f\u4e00\u4e2a\u6ce8\u89e3&quot;;\n}<\/code><\/pre>\n<p>\u5373\u7528<code>@interface<\/code>\u6765\u58f0\u660e\u4e00\u4e2a\u6ce8\u89e3\uff0c\u6ce8\u89e3\u5185\u90e8\u53ef\u4ee5\u6709\u5c5e\u6027\uff0c\u4f46\u4e0d\u80fd\u7528\u65b9\u6cd5\uff0c\u5e76\u4e14\u5c5e\u6027\u7684\u8bbe\u7f6e\u65b9\u5f0f\u4e3a<strong>\u201c\u65e0\u53c2\u7684\u65b9\u6cd5\u201d<\/strong>\uff0c\u53ea\u80fd\u6709\u6570\u636e\u7c7b\u578b\uff0c\u4e0d\u80fd\u8bbe\u7f6e\u8bbf\u95ee\u6743\u9650\uff0c\u53ef\u4ee5\u6307\u5b9a\u9ed8\u8ba4\u503c\u3002<\/p>\n<h5>\u9884\u8bbe\u6ce8\u89e3<\/h5>\n<p>JDK\u9884\u8bbe\u4e86\u4e00\u4e9b\u5e38\u7528\u7684\u6ce8\u89e3\uff0c\u4f5c\u7528\u4e8e\u4ee3\u7801\uff1a<\/p>\n<ul>\n<li>@Override - \u68c0\u67e5\u8be5\u65b9\u6cd5\u662f\u5426\u662f\u91cd\u5199\u65b9\u6cd5\uff0c\u5982\u679c\u5176\u7236\u7c7b\u6216\u8005\u5b9e\u73b0\u7684\u63a5\u53e3\u4e2d\u6ca1\u6709\u8be5\u65b9\u6cd5\u90a3\u4e48\u5c31\u4f1a\u7f16\u8bd1\u62a5\u9519\uff01\u8fdb\u4f5c\u7528\u5728\u7f16\u8bd1\u9636\u6bb5\u3002<\/li>\n<li>@Deprecated - \u6807\u8bb0\u8fc7\u65f6\u65b9\u6cd5\uff0c\u6bd4\u5982Thread.stop()\uff0c\u4f7f\u7528\u8fd9\u7c7b\u65b9\u6cd5\u65f6\u7f16\u8bd1\u5668\u4f1a\u8b66\u544a\u3002<\/li>\n<li>@SuppressWarnings - \u6307\u793a\u7f16\u8bd1\u5668\u53bb\u5ffd\u7565\u6ce8\u89e3\u4e2d\u58f0\u660e\u7684\u8b66\u544a\uff08\u4ec5\u4ec5\u662f\u7f16\u8bd1\u9636\u6bb5\uff09<\/li>\n<li>@FunctionalInterface- \u6807\u8bb0\u63a5\u53e3\u4e3a\u51fd\u6570\u5f0f\u63a5\u53e3\uff0c JDK8\u5f00\u59cb\u652f\u6301<\/li>\n<\/ul>\n<p>\u8fd8\u6709\u5f88\u591a\u3002<\/p>\n<h5>\u5143\u6ce8\u89e3<\/h5>\n<p>\u5143\u6ce8\u89e3\u7528\u4e8e\u6211\u4eec\u7f16\u5199\u7684\u6ce8\u89e3\u7c7b\u578b\u4e0a\uff0c\u7528\u6765\u9650\u5236\u8be5\u6ce8\u89e3\u7684\u4f5c\u7528\u57df\uff1a<\/p>\n<ul>\n<li>@Target({ElementType.METHOD, ElementType.TYPE}) - \u8868\u793a\u8be5\u6ce8\u89e3\u53ea\u80fd\u7528\u4e8eMETHOD\u65b9\u6cd5\u548cTYPE\u7c7b\u578b\u4e0a\u9762\uff0c\u7c7b\u578b\u5305\u62ec\u7c7b\u3001\u63a5\u53e3\u548c\u679a\u4e3e\u3002ElementType\u91cc\u9762\u8fd8\u6709\u5176\u5b83\u7c7b\u578b\uff0c\u9700\u8981\u65f6\u53ef\u4ee5\u6dfb\u52a0\u3002<\/li>\n<li>@Retention(RetentionPolicy.RUNTIME) - \u7528\u4e8e\u6307\u5b9a\u8be5\u6ce8\u89e3\u53ef\u4ee5\u4fdd\u7559\u5230\u4ec0\u4e48\u65f6\u5019\u3002RUNTIME\uff0c\u8fd0\u884c\u65f6\uff1bCLASS\uff0c\u7f16\u8bd1\u4e3a\u5b57\u8282\u7801\u65f6\uff1bSOURCE\uff0c\u7f16\u8bd1\u65f6\u3002\u82e5\u4fdd\u7559\u5230\u8fd0\u884c\u65f6\u53ef\u4ee5\u901a\u8fc7\u53cd\u5c04\u6765\u8bbf\u95ee\u6ce8\u89e3\u3002<\/li>\n<li>@Documented - \u6807\u8bb0\u8fd9\u4e9b\u6ce8\u89e3\u662f\u5426\u5305\u542b\u5728\u7528\u6237\u6587\u6863\u4e2d\u3002<\/li>\n<li>@Inherited - \u6807\u8bb0\u8fd9\u4e2a\u6ce8\u89e3\u662f\u7ee7\u627f\u4e8e\u54ea\u4e2a\u6ce8\u89e3\u7c7b(\u9ed8\u8ba4 \u6ce8\u89e3\u5e76\u6ca1\u6709\u7ee7\u627f\u4e8e\u4efb\u4f55\u5b50\u7c7b)<\/li>\n<\/ul>\n<h5>\u6ce8\u89e3\u7684\u4f7f\u7528<\/h5>\n<p>\u5bf9\u4e8e\u4e0a\u8ff0\u81ea\u5b9a\u4e49\u7684\u6ce8\u89e3\uff0c\u53ef\u4ee5\u7528\u5728\u7c7b\u6216\u8005\u65b9\u6cd5\u4e0a\u9762\uff0c\u540c\u65f6\u4f7f\u7528\u65f6\u5fc5\u987b\u4e3a\u6ce8\u89e3\u7684\u5c5e\u6027\u8fdb\u884c\u8d4b\u503c(\u5c5e\u6027\u6709\u9ed8\u8ba4\u503c\u65f6\u53ef\u4ee5\u7f3a\u7701)\u3002<\/p>\n<pre><code class=\"language-java\">@Test(value = &quot;\u8fd9\u662f\u4e00\u4e2a\u7c7b\u7684\u6ce8\u89e3&quot;)\npublic class Main {\n    @Test(value = &quot;\u8fd9\u662f\u4e00\u4e2a\u65b9\u6cd5\u7684\u6ce8\u89e3&quot;)\n    public static void main(String[] args) {\n        ;\n    }\n}<\/code><\/pre>\n<p>\u6ce8\u89e3\u4fdd\u7559\u5230\u8fd0\u884c\u65f6\u7684\u8bdd\u5c31\u53ef\u4ee5\u901a\u8fc7\u53cd\u5c04\u6765\u83b7\u5f97\u6ce8\u89e3\u3002<\/p>\n<pre><code class=\"language-java\">Class&lt;CookerandConsumer&gt; clazz = CookerandConsumer.class;\n        for (Annotation annotation : clazz.getAnnotations()) {\n            System.out.println(annotation.annotationType());\n            if (annotation instanceof Test) {\n                Test test = (Test) annotation;\n                System.out.println(test.value());\n            }\n        }\n<\/code><\/pre>\n<p>\u6ce8\u89e3\u4e5f\u7b97\u662f\u4e00\u4e2a\u7c7b\uff0c\u90a3\u4e48\u5b83\u5c31\u53ef\u4ee5\u521b\u5efa\u5bf9\u8c61\uff0c\u5bf9\u8c61\u53ef\u4ee5\u8bbf\u95ee\u5176\u5185\u90e8\u7684\u5c5e\u6027\u3002<\/p>\n<p>\u53ef\u4ee5\u83b7\u53d6\u5230\u4efb\u610f\u4f4d\u7f6e\u7684\u6ce8\u89e3\uff0c\u7c7b\u4e0a\uff0c\u63a5\u53e3\u4e0a\uff0c\u65b9\u6cd5\u4e0a\uff0c\u5c5e\u6027\u4e0a\u7b49\u7b49\uff0c\u56e0\u4e3a\u5b83\u4eec\u90fd\u5b9e\u73b0\u4e86AnnotatedElement\u63a5\u53e3\uff0c\u90fd\u6709getAnnotations\u65b9\u6cd5\u3002<\/p>\n<p>\u4e5f\u53ef\u4ee5\u83b7\u53d6\u6307\u5b9a\u7684\u6ce8\u89e3\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/ \u53ea\u60f3\u83b7\u53d6add\u65b9\u6cd5\u4e0a\u7684Test\u6ce8\u89e3\u3002\nSystem.out.println(clazz.getMethod(&quot;add&quot;).getAnnotation(Test.class));<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\u89e3\u672c\u8d28\u4e0a\u4e5f\u662f\u4e00\u4e2a\u7c7b\uff0c\u4f46\u7528\u6cd5\u6bd4\u8f83\u7279\u6b8a\u3002\u6ce8\u89e3\u53ef\u4ee5\u88ab\u6807\u6ce8\u5728\u4efb\u610f\u5730\u65b9\uff0c\u5305\u62ec\u65b9\u6cd5\u4e0a\u3001\u7c7b\u540d\u4e0a\u3001\u53c2\u6570\u4e0a\u3001\u6210\u5458\u5c5e\u6027 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[75],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1442"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=1442"}],"version-history":[{"count":5,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1442\/revisions"}],"predecessor-version":[{"id":1446,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1442\/revisions\/1446"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=1442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=1442"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=1442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}