{"id":561,"date":"2016-10-11T05:03:55","date_gmt":"2016-10-11T05:03:55","guid":{"rendered":"http:\/\/lg.yusemo.com\/?p=561"},"modified":"2016-10-11T05:06:25","modified_gmt":"2016-10-11T05:06:25","slug":"%e6%b3%a8%e8%a7%a3%ef%bc%88annotation%ef%bc%89%e8%87%aa%e5%ae%9a%e4%b9%89%e6%b3%a8%e8%a7%a3%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"https:\/\/lg.yusemo.com\/?p=561","title":{"rendered":"\u6ce8\u89e3\uff08Annotation\uff09\u81ea\u5b9a\u4e49\u6ce8\u89e3\u5165\u95e8\u00a0"},"content":{"rendered":"<div id=\"cnblogs_post_body\">\n<p><strong>\u3000\u3000<\/strong>\u8981\u6df1\u5165\u5b66\u4e60\u6ce8\u89e3\uff0c\u6211\u4eec\u5c31\u5fc5\u987b\u80fd\u5b9a\u4e49\u81ea\u5df1\u7684\u6ce8\u89e3\uff0c\u5e76\u4f7f\u7528\u6ce8\u89e3\uff0c\u5728\u5b9a\u4e49\u81ea\u5df1\u7684\u6ce8\u89e3\u4e4b\u524d\uff0c\u6211\u4eec\u5c31\u5fc5\u987b\u8981\u4e86\u89e3Java\u4e3a\u6211\u4eec\u63d0\u4f9b\u7684\u5143\u6ce8\u89e3\u548c\u76f8\u5173\u5b9a\u4e49\u6ce8\u89e3\u7684\u8bed\u6cd5\u3002<\/p>\n<hr \/>\n<p><strong>\u5143\u6ce8\u89e3\uff1a<\/strong><\/p>\n<p>\u5143\u6ce8\u89e3\u7684\u4f5c\u7528\u5c31\u662f\u8d1f\u8d23\u6ce8\u89e3\u5176\u4ed6\u6ce8\u89e3\u3002Java5.0\u5b9a\u4e49\u4e864\u4e2a\u6807\u51c6\u7684meta-annotation\u7c7b\u578b\uff0c\u5b83\u4eec\u88ab\u7528\u6765\u63d0\u4f9b\u5bf9\u5176\u5b83 annotation\u7c7b\u578b\u4f5c\u8bf4\u660e\u3002Java5.0\u5b9a\u4e49\u7684\u5143\u6ce8\u89e3\uff1a<br \/>\n1.@Target,<br \/>\n2.@Retention,<br \/>\n3.@Documented,<br \/>\n4.@Inherited<br \/>\n\u8fd9\u4e9b\u7c7b\u578b\u548c\u5b83\u4eec\u6240\u652f\u6301\u7684\u7c7b\u5728java.lang.annotation\u5305\u4e2d\u53ef\u4ee5\u627e\u5230\u3002\u4e0b\u9762\u6211\u4eec\u770b\u4e00\u4e0b\u6bcf\u4e2a\u5143\u6ce8\u89e3\u7684\u4f5c\u7528\u548c\u76f8\u5e94\u5206\u53c2\u6570\u7684\u4f7f\u7528\u8bf4\u660e\u3002<\/p>\n<hr \/>\n<p><strong>@Target\uff1a<\/strong><\/p>\n<p>@Target\u8bf4\u660e\u4e86Annotation\u6240\u4fee\u9970\u7684\u5bf9\u8c61\u8303\u56f4\uff1aAnnotation\u53ef\u88ab\u7528\u4e8e packages\u3001types\uff08\u7c7b\u3001\u63a5\u53e3\u3001\u679a\u4e3e\u3001Annotation\u7c7b\u578b\uff09\u3001\u7c7b\u578b\u6210\u5458\uff08\u65b9\u6cd5\u3001\u6784\u9020\u65b9\u6cd5\u3001\u6210\u5458\u53d8\u91cf\u3001\u679a\u4e3e\u503c\uff09\u3001\u65b9\u6cd5\u53c2\u6570\u548c\u672c\u5730\u53d8\u91cf\uff08\u5982\u5faa\u73af\u53d8\u91cf\u3001catch\u53c2\u6570\uff09\u3002\u5728Annotation\u7c7b\u578b\u7684\u58f0\u660e\u4e2d\u4f7f\u7528\u4e86target\u53ef\u66f4\u52a0\u660e\u6670\u5176\u4fee\u9970\u7684\u76ee\u6807\u3002<\/p>\n<p><strong>\u3000<\/strong>\u3000<strong>\u4f5c\u7528\uff1a\u7528\u4e8e\u63cf\u8ff0\u6ce8\u89e3\u7684\u4f7f\u7528\u8303\u56f4\uff08\u5373\uff1a\u88ab\u63cf\u8ff0\u7684\u6ce8\u89e3\u53ef\u4ee5\u7528\u5728\u4ec0\u4e48\u5730\u65b9\uff09<\/strong><\/p>\n<p><strong>\u3000\u53d6\u503c(ElementType)\u6709\uff1a<\/strong><\/p>\n<p>1.CONSTRUCTOR:\u7528\u4e8e\u63cf\u8ff0\u6784\u9020\u5668<br \/>\n2.FIELD:\u7528\u4e8e\u63cf\u8ff0\u57df<br \/>\n3.LOCAL_VARIABLE:\u7528\u4e8e\u63cf\u8ff0\u5c40\u90e8\u53d8\u91cf<br \/>\n4.METHOD:\u7528\u4e8e\u63cf\u8ff0\u65b9\u6cd5<br \/>\n5.PACKAGE:\u7528\u4e8e\u63cf\u8ff0\u5305<br \/>\n6.PARAMETER:\u7528\u4e8e\u63cf\u8ff0\u53c2\u6570<br \/>\n7.TYPE:\u7528\u4e8e\u63cf\u8ff0\u7c7b\u3001\u63a5\u53e3(\u5305\u62ec\u6ce8\u89e3\u7c7b\u578b) \u6216enum\u58f0\u660e<\/p>\n<p>\u4f7f\u7528\u5b9e\u4f8b\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<pre><span style=\"color: #000000;\">@Target(ElementType.TYPE)\n<\/span><span style=\"color: #0000ff;\">public<\/span> @<span style=\"color: #0000ff;\">interface<\/span><span style=\"color: #000000;\"> Table {\n    <\/span><span style=\"color: #008000;\">\/**<\/span><span style=\"color: #008000;\">\n     * \u6570\u636e\u8868\u540d\u79f0\u6ce8\u89e3\uff0c\u9ed8\u8ba4\u503c\u4e3a\u7c7b\u540d\u79f0\n     * <\/span><span style=\"color: #808080;\">@return<\/span>\n     <span style=\"color: #008000;\">*\/<\/span>\n    <span style=\"color: #0000ff;\">public<\/span> String tableName() <span style=\"color: #0000ff;\">default<\/span> \"className\"<span style=\"color: #000000;\">;\n}\n\n@Target(ElementType.FIELD)\n<\/span><span style=\"color: #0000ff;\">public<\/span> @<span style=\"color: #0000ff;\">interface<\/span><span style=\"color: #000000;\"> NoDBColumn {\n\n}<\/span><\/pre>\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<\/div>\n<p>\u6ce8\u89e3Table \u53ef\u4ee5\u7528\u4e8e\u6ce8\u89e3\u7c7b\u3001\u63a5\u53e3(\u5305\u62ec\u6ce8\u89e3\u7c7b\u578b) \u6216enum\u58f0\u660e,\u800c\u6ce8\u89e3NoDBColumn\u4ec5\u53ef\u7528\u4e8e\u6ce8\u89e3\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u3002<\/p>\n<hr \/>\n<p><strong>\u3000\u3000@Retention\uff1a<\/strong><\/p>\n<p><strong>\u3000@Retention<\/strong>\u5b9a\u4e49\u4e86\u8be5Annotation\u88ab\u4fdd\u7559\u7684\u65f6\u95f4\u957f\u77ed\uff1a\u67d0\u4e9bAnnotation\u4ec5\u51fa\u73b0\u5728\u6e90\u4ee3\u7801\u4e2d\uff0c\u800c\u88ab\u7f16\u8bd1\u5668\u4e22\u5f03\uff1b\u800c\u53e6\u4e00\u4e9b\u5374\u88ab\u7f16\u8bd1\u5728class\u6587\u4ef6\u4e2d\uff1b\u7f16\u8bd1\u5728class\u6587\u4ef6\u4e2d\u7684Annotation\u53ef\u80fd\u4f1a\u88ab\u865a\u62df\u673a\u5ffd\u7565\uff0c\u800c\u53e6\u4e00\u4e9b\u5728class\u88ab\u88c5\u8f7d\u65f6\u5c06\u88ab\u8bfb\u53d6\uff08\u8bf7\u6ce8\u610f\u5e76\u4e0d\u5f71\u54cdclass\u7684\u6267\u884c\uff0c\u56e0\u4e3aAnnotation\u4e0eclass\u5728\u4f7f\u7528\u4e0a\u662f\u88ab\u5206\u79bb\u7684\uff09\u3002\u4f7f\u7528\u8fd9\u4e2ameta-Annotation\u53ef\u4ee5\u5bf9 Annotation\u7684\u201c\u751f\u547d\u5468\u671f\u201d\u9650\u5236\u3002<\/p>\n<p><strong>\u4f5c\u7528\uff1a\u8868\u793a\u9700\u8981\u5728\u4ec0\u4e48\u7ea7\u522b\u4fdd\u5b58\u8be5\u6ce8\u91ca\u4fe1\u606f\uff0c\u7528\u4e8e\u63cf\u8ff0\u6ce8\u89e3\u7684\u751f\u547d\u5468\u671f\uff08\u5373\uff1a\u88ab\u63cf\u8ff0\u7684\u6ce8\u89e3\u5728\u4ec0\u4e48\u8303\u56f4\u5185\u6709\u6548\uff09<\/strong><\/p>\n<p><strong>\u3000\u53d6\u503c\uff08RetentionPoicy\uff09\u6709\uff1a<\/strong><\/p>\n<p>1.SOURCE:\u5728\u6e90\u6587\u4ef6\u4e2d\u6709\u6548\uff08\u5373\u6e90\u6587\u4ef6\u4fdd\u7559\uff09<br \/>\n2.CLASS:\u5728class\u6587\u4ef6\u4e2d\u6709\u6548\uff08\u5373class\u4fdd\u7559\uff09<br \/>\n3.RUNTIME:\u5728\u8fd0\u884c\u65f6\u6709\u6548\uff08\u5373\u8fd0\u884c\u65f6\u4fdd\u7559\uff09<\/p>\n<p>Retention meta-annotation\u7c7b\u578b\u6709\u552f\u4e00\u7684value\u4f5c\u4e3a\u6210\u5458\uff0c\u5b83\u7684\u53d6\u503c\u6765\u81eajava.lang.annotation.RetentionPolicy\u7684\u679a\u4e3e\u7c7b\u578b\u503c\u3002\u5177\u4f53\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<pre><span style=\"color: #000000;\">@Target(ElementType.FIELD)\n@Retention(RetentionPolicy.RUNTIME)\n<\/span><span style=\"color: #0000ff;\">public<\/span> @<span style=\"color: #0000ff;\">interface<\/span><span style=\"color: #000000;\"> Column {\n    <\/span><span style=\"color: #0000ff;\">public<\/span> String name() <span style=\"color: #0000ff;\">default<\/span> \"fieldName\"<span style=\"color: #000000;\">;\n    <\/span><span style=\"color: #0000ff;\">public<\/span> String setFuncName() <span style=\"color: #0000ff;\">default<\/span> \"setField\"<span style=\"color: #000000;\">;\n    <\/span><span style=\"color: #0000ff;\">public<\/span> String getFuncName() <span style=\"color: #0000ff;\">default<\/span> \"getField\"<span style=\"color: #000000;\">; \n    <\/span><span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">boolean<\/span> defaultDBValue() <span style=\"color: #0000ff;\">default<\/span> <span style=\"color: #0000ff;\">false<\/span><span style=\"color: #000000;\">;\n}<\/span><\/pre>\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<\/div>\n<p>Column\u6ce8\u89e3\u7684\u7684RetentionPolicy\u7684\u5c5e\u6027\u503c\u662fRUTIME,\u8fd9\u6837\u6ce8\u89e3\u5904\u7406\u5668\u53ef\u4ee5\u901a\u8fc7\u53cd\u5c04\uff0c\u83b7\u53d6\u5230\u8be5\u6ce8\u89e3\u7684\u5c5e\u6027\u503c\uff0c\u4ece\u800c\u53bb\u505a\u4e00\u4e9b\u8fd0\u884c\u65f6\u7684\u903b\u8f91\u5904\u7406<\/p>\n<hr \/>\n<p><strong>\u3000\u3000@Documented:<\/strong><\/p>\n<p><strong>\u3000\u3000<strong>@<\/strong><\/strong>Documented\u7528\u4e8e\u63cf\u8ff0\u5176\u5b83\u7c7b\u578b\u7684annotation\u5e94\u8be5\u88ab\u4f5c\u4e3a\u88ab\u6807\u6ce8\u7684\u7a0b\u5e8f\u6210\u5458\u7684\u516c\u5171API\uff0c\u56e0\u6b64\u53ef\u4ee5\u88ab\u4f8b\u5982javadoc\u6b64\u7c7b\u7684\u5de5\u5177\u6587\u6863\u5316\u3002Documented\u662f\u4e00\u4e2a\u6807\u8bb0\u6ce8\u89e3\uff0c\u6ca1\u6709\u6210\u5458\u3002<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<pre><span style=\"color: #000000;\">@Target(ElementType.FIELD)\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\n<\/span><span style=\"color: #0000ff;\">public<\/span> @<span style=\"color: #0000ff;\">interface<\/span><span style=\"color: #000000;\"> Column {\n    <\/span><span style=\"color: #0000ff;\">public<\/span> String name() <span style=\"color: #0000ff;\">default<\/span> \"fieldName\"<span style=\"color: #000000;\">;\n    <\/span><span style=\"color: #0000ff;\">public<\/span> String setFuncName() <span style=\"color: #0000ff;\">default<\/span> \"setField\"<span style=\"color: #000000;\">;\n    <\/span><span style=\"color: #0000ff;\">public<\/span> String getFuncName() <span style=\"color: #0000ff;\">default<\/span> \"getField\"<span style=\"color: #000000;\">; \n    <\/span><span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">boolean<\/span> defaultDBValue() <span style=\"color: #0000ff;\">default<\/span> <span style=\"color: #0000ff;\">false<\/span><span style=\"color: #000000;\">;\n}<\/span><\/pre>\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<\/div>\n<hr \/>\n<p><strong>\u3000\u3000@Inherited\uff1a<\/strong><\/p>\n<p><strong>\u3000\u3000<\/strong>@Inherited \u5143\u6ce8\u89e3\u662f\u4e00\u4e2a\u6807\u8bb0\u6ce8\u89e3\uff0c@Inherited\u9610\u8ff0\u4e86\u67d0\u4e2a\u88ab\u6807\u6ce8\u7684\u7c7b\u578b\u662f\u88ab\u7ee7\u627f\u7684\u3002\u5982\u679c\u4e00\u4e2a\u4f7f\u7528\u4e86@Inherited\u4fee\u9970\u7684annotation\u7c7b\u578b\u88ab\u7528\u4e8e\u4e00\u4e2aclass\uff0c\u5219\u8fd9\u4e2aannotation\u5c06\u88ab\u7528\u4e8e\u8be5class\u7684\u5b50\u7c7b\u3002<\/p>\n<p>\u6ce8\u610f\uff1a@Inherited annotation\u7c7b\u578b\u662f\u88ab\u6807\u6ce8\u8fc7\u7684class\u7684\u5b50\u7c7b\u6240\u7ee7\u627f\u3002\u7c7b\u5e76\u4e0d\u4ece\u5b83\u6240\u5b9e\u73b0\u7684\u63a5\u53e3\u7ee7\u627fannotation\uff0c\u65b9\u6cd5\u5e76\u4e0d\u4ece\u5b83\u6240\u91cd\u8f7d\u7684\u65b9\u6cd5\u7ee7\u627fannotation\u3002<\/p>\n<p>\u5f53@Inherited annotation\u7c7b\u578b\u6807\u6ce8\u7684annotation\u7684Retention\u662fRetentionPolicy.RUNTIME\uff0c\u5219\u53cd\u5c04API\u589e\u5f3a\u4e86\u8fd9\u79cd\u7ee7\u627f\u6027\u3002\u5982\u679c\u6211\u4eec\u4f7f\u7528java.lang.reflect\u53bb\u67e5\u8be2\u4e00\u4e2a@Inherited annotation\u7c7b\u578b\u7684annotation\u65f6\uff0c\u53cd\u5c04\u4ee3\u7801\u68c0\u67e5\u5c06\u5c55\u5f00\u5de5\u4f5c\uff1a\u68c0\u67e5class\u548c\u5176\u7236\u7c7b\uff0c\u76f4\u5230\u53d1\u73b0\u6307\u5b9a\u7684annotation\u7c7b\u578b\u88ab\u53d1\u73b0\uff0c\u6216\u8005\u5230\u8fbe\u7c7b\u7ee7\u627f\u7ed3\u6784\u7684\u9876\u5c42\u3002<\/p>\n<p>\u5b9e\u4f8b\u4ee3\u7801\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<pre><span style=\"color: #008000;\">\/**<\/span><span style=\"color: #008000;\">\n * \n * <\/span><span style=\"color: #808080;\">@author<\/span><span style=\"color: #008000;\"> peida\n *\n <\/span><span style=\"color: #008000;\">*\/<\/span><span style=\"color: #000000;\">\n@Inherited\n<\/span><span style=\"color: #0000ff;\">public<\/span> @<span style=\"color: #0000ff;\">interface<\/span><span style=\"color: #000000;\"> Greeting {\n    <\/span><span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">enum<\/span><span style=\"color: #000000;\"> FontColor{ BULE,RED,GREEN};\n    String name();\n    FontColor fontColor() <\/span><span style=\"color: #0000ff;\">default<\/span><span style=\"color: #000000;\"> FontColor.GREEN;\n}<\/span><\/pre>\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<\/div>\n<hr \/>\n<p><strong>\u81ea\u5b9a\u4e49\u6ce8\u89e3\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528@interface\u81ea\u5b9a\u4e49\u6ce8\u89e3\u65f6\uff0c\u81ea\u52a8\u7ee7\u627f\u4e86java.lang.annotation.Annotation\u63a5\u53e3\uff0c\u7531\u7f16\u8bd1\u7a0b\u5e8f\u81ea\u52a8\u5b8c\u6210\u5176\u4ed6\u7ec6\u8282\u3002\u5728\u5b9a\u4e49\u6ce8\u89e3\u65f6\uff0c\u4e0d\u80fd\u7ee7\u627f\u5176\u4ed6\u7684\u6ce8\u89e3\u6216\u63a5\u53e3\u3002@interface\u7528\u6765\u58f0\u660e\u4e00\u4e2a\u6ce8\u89e3\uff0c\u5176\u4e2d\u7684\u6bcf\u4e00\u4e2a\u65b9\u6cd5\u5b9e\u9645\u4e0a\u662f\u58f0\u660e\u4e86\u4e00\u4e2a\u914d\u7f6e\u53c2\u6570\u3002\u65b9\u6cd5\u7684\u540d\u79f0\u5c31\u662f\u53c2\u6570\u7684\u540d\u79f0\uff0c\u8fd4\u56de\u503c\u7c7b\u578b\u5c31\u662f\u53c2\u6570\u7684\u7c7b\u578b\uff08\u8fd4\u56de\u503c\u7c7b\u578b\u53ea\u80fd\u662f\u57fa\u672c\u7c7b\u578b\u3001Class\u3001String\u3001enum\uff09\u3002\u53ef\u4ee5\u901a\u8fc7default\u6765\u58f0\u660e\u53c2\u6570\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n<p><strong>\u5b9a\u4e49\u6ce8\u89e3\u683c\u5f0f\uff1a<\/strong><br \/>\npublic\u00a0@interface \u6ce8\u89e3\u540d {\u5b9a\u4e49\u4f53}<\/p>\n<p><em><em>\u3000\u3000<\/em><\/em><strong>\u6ce8\u89e3\u53c2\u6570\u7684\u53ef\u652f\u6301\u6570\u636e\u7c7b\u578b\uff1a<\/strong><\/p>\n<p>1.\u6240\u6709\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08int,float,boolean,byte,double,char,long,short)<br \/>\n2.String\u7c7b\u578b<br \/>\n3.Class\u7c7b\u578b<br \/>\n4.enum\u7c7b\u578b<br \/>\n5.Annotation\u7c7b\u578b<br \/>\n6.\u4ee5\u4e0a\u6240\u6709\u7c7b\u578b\u7684\u6570\u7ec4<\/p>\n<p>Annotation\u7c7b\u578b\u91cc\u9762\u7684\u53c2\u6570\u8be5\u600e\u4e48\u8bbe\u5b9a:<br \/>\n\u7b2c\u4e00,\u53ea\u80fd\u7528public\u6216\u9ed8\u8ba4(default)\u8fd9\u4e24\u4e2a\u8bbf\u95ee\u6743\u4fee\u9970.\u4f8b\u5982,String value();\u8fd9\u91cc\u628a\u65b9\u6cd5\u8bbe\u4e3adefaul\u9ed8\u8ba4\u7c7b\u578b\uff1b<br \/>\n\u7b2c\u4e8c,\u53c2\u6570\u6210\u5458\u53ea\u80fd\u7528\u57fa\u672c\u7c7b\u578bbyte,short,char,int,long,float,double,boolean\u516b\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c String,Enum,Class,annotations\u7b49\u6570\u636e\u7c7b\u578b,\u4ee5\u53ca\u8fd9\u4e00\u4e9b\u7c7b\u578b\u7684\u6570\u7ec4.\u4f8b\u5982,String value();\u8fd9\u91cc\u7684\u53c2\u6570\u6210\u5458\u5c31\u4e3aString;<br \/>\n\u7b2c\u4e09,\u5982\u679c\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u6210\u5458,\u6700\u597d\u628a\u53c2\u6570\u540d\u79f0\u8bbe\u4e3a&#8221;value&#8221;,\u540e\u52a0\u5c0f\u62ec\u53f7.\u4f8b:\u4e0b\u9762\u7684\u4f8b\u5b50FruitName\u6ce8\u89e3\u5c31\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u6210\u5458\u3002<\/p>\n<p>\u7b80\u5355\u7684\u81ea\u5b9a\u4e49\u6ce8\u89e3\u548c\u4f7f\u7528\u6ce8\u89e3\u5b9e\u4f8b\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<pre><span style=\"color: #0000ff;\">package<\/span><span style=\"color: #000000;\"> annotation;\n\n<\/span><span style=\"color: #0000ff;\">import<\/span><span style=\"color: #000000;\"> java.lang.annotation.Documented;\n<\/span><span style=\"color: #0000ff;\">import<\/span><span style=\"color: #000000;\"> java.lang.annotation.ElementType;\n<\/span><span style=\"color: #0000ff;\">import<\/span><span style=\"color: #000000;\"> java.lang.annotation.Retention;\n<\/span><span style=\"color: #0000ff;\">import<\/span><span style=\"color: #000000;\"> java.lang.annotation.RetentionPolicy;\n<\/span><span style=\"color: #0000ff;\">import<\/span><span style=\"color: #000000;\"> java.lang.annotation.Target;\n\n<\/span><span style=\"color: #008000;\">\/**<\/span><span style=\"color: #008000;\">\n * \u6c34\u679c\u540d\u79f0\u6ce8\u89e3\n * <\/span><span style=\"color: #808080;\">@author<\/span><span style=\"color: #008000;\"> peida\n *\n <\/span><span style=\"color: #008000;\">*\/<\/span><span style=\"color: #000000;\">\n@Target(ElementType.FIELD)\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\n<\/span><span style=\"color: #0000ff;\">public<\/span> @<span style=\"color: #0000ff;\">interface<\/span><span style=\"color: #000000;\"> FruitName {\n    String value() <\/span><span style=\"color: #0000ff;\">default<\/span> \"\"<span style=\"color: #000000;\">;\n}<\/span><\/pre>\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<\/div>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<pre><span style=\"color: #0000ff;\">package<\/span><span style=\"color: #000000;\"> annotation;\n\n<\/span><span style=\"color: #0000ff;\">import<\/span><span style=\"color: #000000;\"> java.lang.annotation.Documented;\n<\/span><span style=\"color: #0000ff;\">import<\/span><span style=\"color: #000000;\"> java.lang.annotation.ElementType;\n<\/span><span style=\"color: #0000ff;\">import<\/span><span style=\"color: #000000;\"> java.lang.annotation.Retention;\n<\/span><span style=\"color: #0000ff;\">import<\/span><span style=\"color: #000000;\"> java.lang.annotation.RetentionPolicy;\n<\/span><span style=\"color: #0000ff;\">import<\/span><span style=\"color: #000000;\"> java.lang.annotation.Target;\n\n<\/span><span style=\"color: #008000;\">\/**<\/span><span style=\"color: #008000;\">\n * \u6c34\u679c\u989c\u8272\u6ce8\u89e3\n * <\/span><span style=\"color: #808080;\">@author<\/span><span style=\"color: #008000;\"> peida\n *\n <\/span><span style=\"color: #008000;\">*\/<\/span><span style=\"color: #000000;\">\n@Target(ElementType.FIELD)\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\n<\/span><span style=\"color: #0000ff;\">public<\/span> @<span style=\"color: #0000ff;\">interface<\/span><span style=\"color: #000000;\"> FruitColor {\n    <\/span><span style=\"color: #008000;\">\/**<\/span><span style=\"color: #008000;\">\n     * \u989c\u8272\u679a\u4e3e\n     * <\/span><span style=\"color: #808080;\">@author<\/span><span style=\"color: #008000;\"> peida\n     *\n     <\/span><span style=\"color: #008000;\">*\/<\/span>\n    <span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">enum<\/span><span style=\"color: #000000;\"> Color{ BULE,RED,GREEN};\n    \n    <\/span><span style=\"color: #008000;\">\/**<\/span><span style=\"color: #008000;\">\n     * \u989c\u8272\u5c5e\u6027\n     * <\/span><span style=\"color: #808080;\">@return<\/span>\n     <span style=\"color: #008000;\">*\/<\/span><span style=\"color: #000000;\">\n    Color fruitColor() <\/span><span style=\"color: #0000ff;\">default<\/span><span style=\"color: #000000;\"> Color.GREEN;\n\n}<\/span><\/pre>\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<\/div>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<pre><span style=\"color: #0000ff;\">package<\/span><span style=\"color: #000000;\"> annotation;\n\n<\/span><span style=\"color: #0000ff;\">import<\/span><span style=\"color: #000000;\"> annotation.FruitColor.Color;\n\n<\/span><span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">class<\/span><span style=\"color: #000000;\"> Apple {\n    \n    @FruitName(<\/span>\"Apple\"<span style=\"color: #000000;\">)\n    <\/span><span style=\"color: #0000ff;\">private<\/span><span style=\"color: #000000;\"> String appleName;\n    \n    @FruitColor(fruitColor<\/span>=<span style=\"color: #000000;\">Color.RED)\n    <\/span><span style=\"color: #0000ff;\">private<\/span><span style=\"color: #000000;\"> String appleColor;\n    \n    \n    \n    \n    <\/span><span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">void<\/span><span style=\"color: #000000;\"> setAppleColor(String appleColor) {\n        <\/span><span style=\"color: #0000ff;\">this<\/span>.appleColor =<span style=\"color: #000000;\"> appleColor;\n    }\n    <\/span><span style=\"color: #0000ff;\">public<\/span><span style=\"color: #000000;\"> String getAppleColor() {\n        <\/span><span style=\"color: #0000ff;\">return<\/span><span style=\"color: #000000;\"> appleColor;\n    }\n    \n    \n    <\/span><span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">void<\/span><span style=\"color: #000000;\"> setAppleName(String appleName) {\n        <\/span><span style=\"color: #0000ff;\">this<\/span>.appleName =<span style=\"color: #000000;\"> appleName;\n    }\n    <\/span><span style=\"color: #0000ff;\">public<\/span><span style=\"color: #000000;\"> String getAppleName() {\n        <\/span><span style=\"color: #0000ff;\">return<\/span><span style=\"color: #000000;\"> appleName;\n    }\n    \n    <\/span><span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">void<\/span><span style=\"color: #000000;\"> displayName(){\n        System.out.println(<\/span>\"\u6c34\u679c\u7684\u540d\u5b57\u662f\uff1a\u82f9\u679c\"<span style=\"color: #000000;\">);\n    }\n}<\/span><\/pre>\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<\/div>\n<hr \/>\n<p><strong>\u6ce8\u89e3\u5143\u7d20\u7684\u9ed8\u8ba4\u503c\uff1a<\/strong><\/p>\n<p><em id=\"__mceDel\" style=\"line-height: 1.5;\">\u3000\u3000<\/em>\u6ce8\u89e3\u5143\u7d20\u5fc5\u987b\u6709\u786e\u5b9a\u7684\u503c\uff0c\u8981\u4e48\u5728\u5b9a\u4e49\u6ce8\u89e3\u7684\u9ed8\u8ba4\u503c\u4e2d\u6307\u5b9a\uff0c\u8981\u4e48\u5728\u4f7f\u7528\u6ce8\u89e3\u65f6\u6307\u5b9a\uff0c\u975e\u57fa\u672c\u7c7b\u578b\u7684\u6ce8\u89e3\u5143\u7d20\u7684\u503c\u4e0d\u53ef\u4e3anull\u3002\u56e0\u6b64, \u4f7f\u7528\u7a7a\u5b57\u7b26\u4e32\u62160\u4f5c\u4e3a\u9ed8\u8ba4\u503c\u662f\u4e00\u79cd\u5e38\u7528\u7684\u505a\u6cd5\u3002\u8fd9\u4e2a\u7ea6\u675f\u4f7f\u5f97\u5904\u7406\u5668\u5f88\u96be\u8868\u73b0\u4e00\u4e2a\u5143\u7d20\u7684\u5b58\u5728\u6216\u7f3a\u5931\u7684\u72b6\u6001\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u6ce8\u89e3\u7684\u58f0\u660e\u4e2d\uff0c\u6240\u6709\u5143\u7d20\u90fd\u5b58\u5728\uff0c\u5e76\u4e14\u90fd\u5177\u6709\u76f8\u5e94\u7684\u503c\uff0c\u4e3a\u4e86\u7ed5\u5f00\u8fd9\u4e2a\u7ea6\u675f\uff0c\u6211\u4eec\u53ea\u80fd\u5b9a\u4e49\u4e00\u4e9b\u7279\u6b8a\u7684\u503c\uff0c\u4f8b\u5982\u7a7a\u5b57\u7b26\u4e32\u6216\u8005\u8d1f\u6570\uff0c\u4e00\u6b21\u8868\u793a\u67d0\u4e2a\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5728\u5b9a\u4e49\u6ce8\u89e3\u65f6\uff0c\u8fd9\u5df2\u7ecf\u6210\u4e3a\u4e00\u4e2a\u4e60\u60ef\u7528\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<pre><span style=\"color: #008080;\"> 1<\/span> <span style=\"color: #0000ff;\">package<\/span><span style=\"color: #000000;\"> annotation;\n<\/span><span style=\"color: #008080;\"> 2<\/span> \n<span style=\"color: #008080;\"> 3<\/span> <span style=\"color: #0000ff;\">import<\/span><span style=\"color: #000000;\"> java.lang.annotation.Documented;\n<\/span><span style=\"color: #008080;\"> 4<\/span> <span style=\"color: #0000ff;\">import<\/span><span style=\"color: #000000;\"> java.lang.annotation.ElementType;\n<\/span><span style=\"color: #008080;\"> 5<\/span> <span style=\"color: #0000ff;\">import<\/span><span style=\"color: #000000;\"> java.lang.annotation.Retention;\n<\/span><span style=\"color: #008080;\"> 6<\/span> <span style=\"color: #0000ff;\">import<\/span><span style=\"color: #000000;\"> java.lang.annotation.RetentionPolicy;\n<\/span><span style=\"color: #008080;\"> 7<\/span> <span style=\"color: #0000ff;\">import<\/span><span style=\"color: #000000;\"> java.lang.annotation.Target;\n<\/span><span style=\"color: #008080;\"> 8<\/span> \n<span style=\"color: #008080;\"> 9<\/span> <span style=\"color: #008000;\">\/**<\/span>\n<span style=\"color: #008080;\">10<\/span> <span style=\"color: #008000;\"> * \u6c34\u679c\u4f9b\u5e94\u8005\u6ce8\u89e3\n<\/span><span style=\"color: #008080;\">11<\/span> <span style=\"color: #008000;\"> * <\/span><span style=\"color: #808080;\">@author<\/span><span style=\"color: #008000;\"> peida\n<\/span><span style=\"color: #008080;\">12<\/span> <span style=\"color: #008000;\"> *\n<\/span><span style=\"color: #008080;\">13<\/span>  <span style=\"color: #008000;\">*\/<\/span>\n<span style=\"color: #008080;\">14<\/span> <span style=\"color: #000000;\">@Target(ElementType.FIELD)\n<\/span><span style=\"color: #008080;\">15<\/span> <span style=\"color: #000000;\">@Retention(RetentionPolicy.RUNTIME)\n<\/span><span style=\"color: #008080;\">16<\/span> <span style=\"color: #000000;\">@Documented\n<\/span><span style=\"color: #008080;\">17<\/span> <span style=\"color: #0000ff;\">public<\/span> @<span style=\"color: #0000ff;\">interface<\/span><span style=\"color: #000000;\"> FruitProvider {\n<\/span><span style=\"color: #008080;\">18<\/span>     <span style=\"color: #008000;\">\/**<\/span>\n<span style=\"color: #008080;\">19<\/span> <span style=\"color: #008000;\">     * \u4f9b\u5e94\u5546\u7f16\u53f7\n<\/span><span style=\"color: #008080;\">20<\/span> <span style=\"color: #008000;\">     * <\/span><span style=\"color: #808080;\">@return<\/span>\n<span style=\"color: #008080;\">21<\/span>      <span style=\"color: #008000;\">*\/<\/span>\n<span style=\"color: #008080;\">22<\/span>     <span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">int<\/span> id() <span style=\"color: #0000ff;\">default<\/span> -1<span style=\"color: #000000;\">;\n<\/span><span style=\"color: #008080;\">23<\/span>     \n<span style=\"color: #008080;\">24<\/span>     <span style=\"color: #008000;\">\/**<\/span>\n<span style=\"color: #008080;\">25<\/span> <span style=\"color: #008000;\">     * \u4f9b\u5e94\u5546\u540d\u79f0\n<\/span><span style=\"color: #008080;\">26<\/span> <span style=\"color: #008000;\">     * <\/span><span style=\"color: #808080;\">@return<\/span>\n<span style=\"color: #008080;\">27<\/span>      <span style=\"color: #008000;\">*\/<\/span>\n<span style=\"color: #008080;\">28<\/span>     <span style=\"color: #0000ff;\">public<\/span> String name() <span style=\"color: #0000ff;\">default<\/span> \"\"<span style=\"color: #000000;\">;\n<\/span><span style=\"color: #008080;\">29<\/span>     \n<span style=\"color: #008080;\">30<\/span>     <span style=\"color: #008000;\">\/**<\/span>\n<span style=\"color: #008080;\">31<\/span> <span style=\"color: #008000;\">     * \u4f9b\u5e94\u5546\u5730\u5740\n<\/span><span style=\"color: #008080;\">32<\/span> <span style=\"color: #008000;\">     * <\/span><span style=\"color: #808080;\">@return<\/span>\n<span style=\"color: #008080;\">33<\/span>      <span style=\"color: #008000;\">*\/<\/span>\n<span style=\"color: #008080;\">34<\/span>     <span style=\"color: #0000ff;\">public<\/span> String address() <span style=\"color: #0000ff;\">default<\/span> \"\"<span style=\"color: #000000;\">;\n<\/span><span style=\"color: #008080;\">35<\/span> }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>\u5b9a\u4e49\u4e86\u6ce8\u89e3\uff0c\u5e76\u5728\u9700\u8981\u7684\u65f6\u5019\u7ed9\u76f8\u5173\u7c7b\uff0c\u7c7b\u5c5e\u6027\u52a0\u4e0a\u6ce8\u89e3\u4fe1\u606f\uff0c\u5982\u679c\u6ca1\u6709\u54cd\u5e94\u7684\u6ce8\u89e3\u4fe1\u606f\u5904\u7406\u6d41\u7a0b\uff0c\u6ce8\u89e3\u53ef\u4ee5\u8bf4\u662f\u6ca1\u6709\u5b9e\u7528\u4ef7\u503c\u3002\u5982\u4f55\u8ba9\u6ce8\u89e3\u771f\u771f\u7684\u53d1\u6325\u4f5c\u7528\uff0c\u4e3b\u8981\u5c31\u5728\u4e8e\u6ce8\u89e3\u5904\u7406\u65b9\u6cd5\uff0c\u4e0b\u4e00\u6b65\u6211\u4eec\u5c06\u5b66\u4e60\u6ce8\u89e3\u4fe1\u606f\u7684\u83b7\u53d6\u548c\u5904\u7406\uff01<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u3000\u3000\u8981\u6df1\u5165\u5b66\u4e60\u6ce8\u89e3\uff0c\u6211\u4eec\u5c31\u5fc5\u987b\u80fd\u5b9a\u4e49\u81ea\u5df1\u7684\u6ce8\u89e3\uff0c\u5e76\u4f7f\u7528\u6ce8\u89e3\uff0c\u5728\u5b9a\u4e49\u81ea\u5df1\u7684\u6ce8\u89e3\u4e4b\u524d\uff0c\u6211\u4eec\u5c31\u5fc5\u987b\u8981\u4e86\u89e3Java\u4e3a\u6211\u4eec\u63d0\u4f9b\u7684\u5143\u6ce8\u89e3\u548c\u76f8\u5173\u5b9a\u4e49\u6ce8\u89e3\u7684\u8bed\u6cd5\u3002 \u5143\u6ce8\u89e3\uff1a \u5143\u6ce8\u89e3\u7684\u4f5c\u7528\u5c31\u662f\u8d1f\u8d23\u6ce8\u89e3\u5176\u4ed6\u6ce8\u89e3\u3002Java5.0\u5b9a\u4e49\u4e864\u4e2a\u6807\u51c6\u7684meta-annotation\u7c7b\u578b\uff0c\u5b83\u4eec\u88ab\u7528\u6765\u63d0\u4f9b\u5bf9\u5176\u5b83 annotation&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22],"tags":[3],"_links":{"self":[{"href":"https:\/\/lg.yusemo.com\/index.php?rest_route=\/wp\/v2\/posts\/561"}],"collection":[{"href":"https:\/\/lg.yusemo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lg.yusemo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lg.yusemo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lg.yusemo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=561"}],"version-history":[{"count":3,"href":"https:\/\/lg.yusemo.com\/index.php?rest_route=\/wp\/v2\/posts\/561\/revisions"}],"predecessor-version":[{"id":649,"href":"https:\/\/lg.yusemo.com\/index.php?rest_route=\/wp\/v2\/posts\/561\/revisions\/649"}],"wp:attachment":[{"href":"https:\/\/lg.yusemo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lg.yusemo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lg.yusemo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}