{"id":78300,"date":"2026-02-26T23:43:12","date_gmt":"2026-02-26T15:43:12","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/78300.html"},"modified":"2026-02-26T23:43:12","modified_gmt":"2026-02-26T15:43:12","slug":"%e3%80%90%e4%bb%8e0%e5%bc%80%e5%a7%8b%e5%ad%a6%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f-4-%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8f%e3%80%91","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/78300.html","title":{"rendered":"\u3010\u4ece0\u5f00\u59cb\u5b66\u8bbe\u8ba1\u6a21\u5f0f-4| \u5355\u4f8b\u6a21\u5f0f\u3011"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226154306-69a06a0ab6ff0.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>Tip&#xff1a;\u672c\u7bc7\u7684\u4ee3\u7801\u5b9e\u73b0\u90fd\u662f\u57fa\u4e8eJava\u7684\u5b9e\u73b0\u3002<\/p>\n<p>\u800c\u4e14\u7531\u4e8e\u4e0d\u540c\u8bed\u8a00\u7684\u7279\u6027\u4e0d\u540c\u3002\u4f8b\u5982c&#043;&#043;\u8ddfjava\u7684\u5185\u5b58\u7ba1\u7406\u662f\u4e0d\u4e00\u6837\u7684&#xff0c;\u8bed\u6cd5\u4e0a\u4e5f\u7565\u6709\u5dee\u522b&#xff0c;\u6240\u4ee5\u5355\u4f8b\u6a21\u5f0f\u7684\u5b9e\u73b0\u4e5f\u6709\u5dee\u522b\u3002\u4f8b\u5982C&#043;&#043;\u7684\u997f\u6c49\u6a21\u5f0f\u53ef\u4ee5\u6709&#034;\u975e\u5c40\u90e8\u9759\u6001\u5b9e\u73b0&#034;\u3001&#034;\u5c40\u90e8\u9759\u6001\u5b9e\u73b0&#034;\u7b49\u7b49&#xff0c;\u5982\u679c\u60f3\u4e86\u89e3C&#043;&#043;\u7248\u672c\u7684\u5355\u4f8b\u6a21\u5f0f\u81ea\u884c\u767e\u5ea6\u5373\u53ef<\/p>\n<h3>\u6982\u8ff0<\/h3>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u8f6f\u4ef6\u7cfb\u7edf\u7684\u67d0\u4e9b\u7c7b\u800c\u8a00&#xff0c;\u6211\u4eec\u65e0\u987b\u521b\u5efa\u591a\u4e2a\u5b9e\u4f8b\u3002\u4e3a\u4e86\u8282\u7ea6\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u6709\u65f6\u9700\u8981\u786e\u4fdd\u7cfb\u7edf\u4e2d\u67d0\u4e2a\u7c7b\u53ea\u6709\u552f\u4e00\u4e00\u4e2a\u5b9e\u4f8b&#xff0c;\u5f53\u8fd9\u4e2a\u552f\u4e00\u5b9e\u4f8b\u521b\u5efa\u6210\u529f\u4e4b\u540e&#xff0c;\u6211\u4eec\u65e0\u6cd5\u518d\u521b\u5efa\u4e00\u4e2a\u540c\u7c7b\u578b\u7684\u5176\u4ed6\u5bf9\u8c61&#xff0c;\u6240\u6709\u7684\u64cd\u4f5c\u90fd\u53ea\u80fd\u57fa\u4e8e\u8fd9\u4e2a\u552f\u4e00\u5b9e\u4f8b\u3002\u4e3a\u4e86\u786e\u4fdd\u5bf9\u8c61\u7684\u552f\u4e00\u6027&#xff0c;\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5355\u4f8b\u6a21\u5f0f&#xff08;Singleton Pattern&#xff09; \u6765\u5b9e\u73b0&#xff0c;\u8fd9\u5c31\u662f\u5355\u4f8b\u6a21\u5f0f\u7684\u52a8\u673a\u6240\u5728\u3002<\/p>\n<p>\u5b9a\u4e49&#xff1a;\u786e\u4fdd\u67d0\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b&#xff0c;\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5168\u5c40\u7684\u8bbf\u95ee\u70b9\u6765\u8bbf\u95ee\u8fd9\u4e2a\u5b9e\u4f8b\u3002<\/p>\n<h3>\u7ed3\u6784<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226154310-69a06a0e33b91.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u53ea\u5305\u542b\u4e00\u4e2a\u5355\u4f8b\u89d2\u8272:<\/p>\n<ul>\n<li>Singleton&#xff08;\u5355\u4f8b&#xff09;: \u5728\u5355\u4f8b\u7c7b\u7684\u5185\u90e8\u5b9e\u73b0\u53ea\u751f\u6210\u4e00\u4e2a\u5b9e\u4f8b&#xff0c;\u540c\u65f6\u5b83\u63d0\u4f9b\u4e00\u4e2a\u9759\u6001\u7684getInstance()\u5de5\u5382\u65b9\u6cd5&#xff0c;\u8ba9\u5ba2\u6237\u53ef\u4ee5\u8bbf\u95ee\u5b83\u7684\u552f\u4e00\u5b9e\u4f8b&#xff1b;\u4e3a\u4e86\u9632\u6b62\u5728\u5916\u90e8\u5bf9\u5176\u5b9e\u4f8b\u5316&#xff0c;\u5c06\u5176\u6784\u9020\u51fd\u6570\u8bbe\u8ba1\u4e3a\u79c1\u6709&#xff1b;\u5728\u5355\u4f8b\u7c7b\u5185\u90e8\u5b9a\u4e49\u4e86\u4e00\u4e2aSingleton\u7c7b\u578b\u7684\u9759\u6001\u5bf9\u8c61&#xff0c;\u4f5c\u4e3a\u5916\u90e8\u5171\u4eab\u7684\u552f\u4e00\u5b9e\u4f8b\u3002<\/li>\n<\/ul>\n<h3>\u5b9e\u73b0<\/h3>\n<h4>\u997f\u6c49\u6a21\u5f0f<\/h4>\n<p>\u997f\u6c49\u6a21\u5f0f\u7b80\u5355\u6765\u8bf4\u5c31\u662f\u63d0\u524d\u51c6\u5907\u597d\u8fd9\u4e2a\u5355\u4f8b&#xff0c;\u4e0d\u7ba1\u4f60\u4e4b\u540e\u7528\u4e0d\u7528\u3002<\/p>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u6700\u7b80\u5355&#xff0c;\u4e5f\u6ca1\u6709\u5e76\u53d1\u95ee\u9898\u548c\u6548\u7387\u95ee\u9898&#xff0c;\u4f46\u662f\u5728\u7c7b\u52a0\u8f7d\u65f6\u5c31\u521d\u59cb\u5316&#xff0c;\u6709\u4e9b\u6d6a\u8d39\u5185\u5b58&#xff0c;\u56e0\u4e3a\u6709\u53ef\u80fd\u8fd9\u4e2a\u65b9\u6cd5\u81ea\u59cb\u81f3\u7ec8\u90fd\u4e0d\u4f1a\u88ab\u8c03\u7528\u5230&#xff0c;\u5c24\u5176\u662f\u5728\u4e00\u4e9b\u5bf9\u5916\u63d0\u4f9b\u7684\u5de5\u5177\u5305\u6216 API \u65f6\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u8fd9\u79cd\u65b9\u5f0f\u3002<\/p>\n<p>Java\u4e2d&#xff0c;\u6700\u7b80\u6d01\u3001\u6700\u73b0\u4ee3\u7684\u997f\u6c49\u5f0f\u5b9e\u73b0\u65b9\u5f0f&#xff0c;\u5c31\u662f\u679a\u4e3e\u5355\u4f8b\u3002\u8fd9\u79cd\u65b9\u5f0f\u5229\u7528\u4e86\u5229\u7528\u4e86\u7c7b\u52a0\u8f7d\u673a\u5236\u4ee5\u53caJVM\u673a\u5236&#xff0c;\u5e2e\u52a9\u6211\u4eec\u89e3\u51b3\u4e86\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u4ee5\u53ca\u62e6\u622a\u53cd\u5c04\u95ee\u9898&#xff0c;\u4e0b\u9762\u770b\u4ee3\u7801&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u679a\u4e3e\u5355\u4f8b\u7c7b<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">enum<\/span> <span class=\"token class-name\">SingletonEnum<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token comment\">\/***\/<\/span><br \/>\n  INSTANCE<span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u679a\u4e3e\u5e38\u91cf public static final\u7c7b\u578b<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<\/li>\n<li>\n<p>\u6d4b\u8bd5\u7c7b<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">ReflectInjectionEnum<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token comment\">\/*<br \/>\n     Lombok \u5e93\u63d0\u4f9b\u7684\u4e00\u4e2a\u201c\u9ed1\u79d1\u6280\u201d\u6ce8\u89e3<br \/>\n     \u80fd\u8ba9\u4f60\u5728\u4e0d\u5199 try-catch \u6216 throws \u58f0\u660e\u7684\u60c5\u51b5\u4e0b&#xff0c;\u629b\u51fa\u53d7\u68c0\u5f02\u5e38&#xff08;Checked Exceptions&#xff09;\u3002<br \/>\n     \u8ba9\u4f60\u7684\u65b9\u6cd5\u770b\u8d77\u6765\u5e72\u51c0\u7b80\u6d01<br \/>\n   *\/<\/span><br \/>\n  <span class=\"token annotation punctuation\">&#064;SneakyThrows<\/span><br \/>\n  <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> args<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">throws<\/span> <span class=\"token class-name\">Exception<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/\u83b7\u53d6\u5230\u7c7b\u5b9a\u4e49&#xff1a;\u83b7\u53d6 SingletonEnum \u5bf9\u5e94\u7684 Class \u5bf9\u8c61&#xff0c;\u662f\u53cd\u5c04\u7684\u5165\u53e3&#xff0c;\u5305\u542b\u4e86\u7c7b\u7684\u6240\u6709\u7ed3\u6784\u4fe1\u606f<\/span><br \/>\n    <span class=\"token class-name\">Class<\/span> x <span class=\"token operator\">&#061;<\/span> <span class=\"token class-name\">SingletonEnum<\/span><span class=\"token punctuation\">.<\/span><span class=\"token keyword\">class<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u83b7\u53d6\u6784\u9020\u5668&#xff1a;\u83b7\u53d6\u679a\u4e3e\u7c7b\u5b9a\u4e49\u7684\u7b2c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/\u6ce8\u610f&#xff1a;\u679a\u4e3e\u9ed8\u8ba4\u6709\u4e00\u4e2a\u9690\u85cf\u7684\u79c1\u6709\u6784\u9020\u51fd\u6570 (String name, int ordinal)\u3002<\/span><br \/>\n    <span class=\"token class-name\">Constructor<\/span> constructor <span class=\"token operator\">&#061;<\/span> x<span class=\"token punctuation\">.<\/span><span class=\"token function\">getDeclaredConstructors<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u66b4\u529b\u7834\u89e3\u6743\u9650&#xff1a;\u5f3a\u884c\u5c06\u79c1\u6709\u7684\u6784\u9020\u51fd\u6570\u8bbe\u7f6e\u4e3a\u53ef\u8bbf\u95ee&#xff0c;\u8fd9\u662f\u53cd\u5c04\u653b\u51fb\u7684\u6807\u914d<\/span><br \/>\n    constructor<span class=\"token punctuation\">.<\/span><span class=\"token function\">setAccessible<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">true<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u5c1d\u8bd5\u8c03\u7528\u6784\u9020\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u5168\u65b0\u7684\u5bf9\u8c61\u3002<\/span><br \/>\n    <span class=\"token class-name\">SingletonEnum<\/span> o <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">SingletonEnum<\/span><span class=\"token punctuation\">)<\/span> constructor<span class=\"token punctuation\">.<\/span><span class=\"token function\">newInstance<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u83b7\u53d6\u5355\u4f8b<\/span><br \/>\n    <span class=\"token class-name\">SingletonEnum<\/span> instance <span class=\"token operator\">&#061;<\/span> <span class=\"token class-name\">SingletonEnum<\/span><span class=\"token punctuation\">.<\/span>INSTANCE<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/\u6bd4\u8f83\u5355\u4f8b\u4e0e\u521d\u59cb\u5316\u7684\u5b9e\u4f8b<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>instance <span class=\"token operator\">&#061;&#061;<\/span> o<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n      <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u76f8\u540c\u7684\u5b9e\u4f8b&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n      <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u4e0d\u540c\u7684\u5b9e\u4f8b&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<\/li>\n<li>\n<p>\u8fd0\u884c\u7ed3\u679c<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226154310-69a06a0e833e0.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<\/li>\n<\/ul>\n<h5>\u89e3\u91ca<\/h5>\n<li>\n<p>\u679a\u4e3e\u7c7b\u600e\u4e48\u4fdd\u8bc1\u4e86\u5355\u4f8b\u7684\u5b9e\u73b0&#xff1f;<\/p>\n<ul>\n<li>\u679a\u4e3e\u7c7bSingletonEnum\u4f1a\u53d8\u6210public final class SingletonEnum extends Enum<\/li>\n<li>\u679a\u4e3e\u7c7b\u91cc\u9762\u7684\u6210\u5458\u5728\u7f16\u8bd1\u9636\u6bb5\u4f1a\u53d8\u6210public static final\u8fd9\u79cd\u7c7b\u578b\u7684\u53d8\u91cf<\/li>\n<li>\u800c\u4e14\u4f1a\u81ea\u52a8\u751f\u6210\u4e00\u4e2a static {} \u9759\u6001\u4ee3\u7801\u5757&#xff0c;\u91cc\u9762\u5305\u542b\u4e86 INSTANCE &#061; new SingletonEnum();<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u7c7b\u52a0\u8f7d\u673a\u5236\u600e\u4e48\u4fdd\u8bc1\u4e86\u552f\u4e00\u5b9e\u4f8b\u4ee5\u53ca\u591a\u7ebf\u7a0b\u5b89\u5168&#xff1f;<\/p>\n<ul>\n<li>\u7f16\u8bd1\u5e76\u4e0d\u89e6\u53d1\u7c7b\u52a0\u8f7d&#xff0c;\u53ea\u6709\u5f53\u8fd0\u884c\u7684\u65f6\u5019&#xff0c;\u4e14\u6709\u4eba\u7b2c\u4e00\u6b21\u78b0\u8fd9\u4e2a\u7c7b&#xff0c;\u5b83\u624d\u52a0\u8f7d\u521d\u59cb\u5316\u3002<\/li>\n<li>\u5f53\u7a0b\u5e8f\u7b2c\u4e00\u6b21\u6267\u884c\u5230 SingletonEnum.INSTANCE \u65f6&#xff0c;JVM \u624d\u4f1a\u89e6\u53d1\u7c7b\u52a0\u8f7d&#xff1a;\n<ul>\n<li>JVM\u8bfb\u53d6.class\u5b57\u8282\u7801\u6587\u4ef6&#xff0c;\u7136\u540e\u4e3a\u9759\u6001\u53d8\u91cf\u5206\u914d\u5185\u5b58\u5e76\u8bbe\u7f6e\u9ed8\u8ba4\u7684\u521d\u59cb\u503c<\/li>\n<li>\u7136\u540e\u6267\u884c\u90a3\u4e2a\u9690\u85cf\u7684 static {} \u9759\u6001\u4ee3\u7801\u5757&#xff0c;JVM\u5185\u90e8\u6709\u4e00\u628a\u521d\u59cb\u5316\u9501&#xff0c;\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u6267\u884c new SingletonEnum()&#xff0c;\u5176\u4ed6\u7ebf\u7a0b\u4f1a\u963b\u585e\u7b49\u5f85\u3002\u7b49\u5230\u521d\u59cb\u5316\u5b8c\u6210\u4e4b\u540e&#xff0c;\u5176\u4ed6\u7ebf\u7a0b\u518d\u60f3\u83b7\u53d6INSTANCE\u8fd9\u4e2a\u5bf9\u8c61&#xff0c;\u5c31\u4f1a\u53d1\u73b0\u4ee5\u53ca\u521d\u59cb\u5316\u5b8c\u6bd5\u4e86&#xff01;<\/li>\n<li>\u8fd9\u6837\u5c31\u4fdd\u8bc1\u4e86INSTANCE\u5bf9\u8c61\u53ea\u6709\u4e00\u4e2a&#xff0c;\u4e14\u521b\u5efa\u8fc7\u7a0b\u7edd\u5bf9\u7ebf\u7a0b\u5b89\u5168\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>JVM\u600e\u4e48\u505a\u5230\u4e86\u62e6\u622a\u53cd\u5c04&#xff1f;<\/p>\n<ul>\n<li>\n<p>\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d&#xff0c;constructor.newInstance() \u88ab\u8c03\u7528\u662f\u53cd\u5c04\u521b\u5efa\u5bf9\u8c61\u7684\u5c1d\u8bd5\u3002<\/p>\n<\/li>\n<li>\n<p>\u6267\u884c newInstance() \u65f6&#xff0c;JVM \u5185\u90e8\u4f1a\u8fdb\u884c\u4e00\u6b21\u5224\u5b9a&#xff1a;\u68c0\u67e5\u8be5\u7c7b\u7684\u4fee\u9970\u7b26\u662f\u5426\u5305\u542b ENUM \u6807\u5fd7\u4f4d\u3002<\/p>\n<\/li>\n<li>\n<p>\u7531\u4e8e SingletonEnum \u5e26\u6709\u679a\u4e3e\u6807\u5fd7&#xff0c;JVM\u4e0d\u4f1a\u6267\u884c&#xff0c;\u5e76\u629b\u51fa IllegalArgumentException\u3002<\/p>\n<p><span class=\"token comment\">\/\/ java.lang.reflect.Constructor \u6e90\u7801\u7247\u6bb5<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span>clazz<span class=\"token punctuation\">.<\/span><span class=\"token function\">getModifiers<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&amp;<\/span> <span class=\"token class-name\">Modifier<\/span><span class=\"token punctuation\">.<\/span>ENUM<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">!&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">throw<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">IllegalArgumentException<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Cannot reflectively create enum objects&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<\/li>\n<li>\n<p>\u6240\u4ee5\u624d\u4f1a\u6709\u4e0a\u9762\u90a3\u6837\u7684\u8fd0\u884c\u7ed3\u679c&#xff0c;\u8fd9\u6210\u529f\u62e6\u622a\u4e86\u53cd\u5c04&#xff0c;\u4fdd\u8bc1\u4e86\u5b89\u5168\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<h4>\u61d2\u6c49\u6a21\u5f0f<\/h4>\n<p>\u61d2\u6c49\u6a21\u5f0f\u662f\u5728\u7b2c\u4e00\u6b21\u4f7f\u7528\u5355\u4f8b\u5bf9\u8c61\u65f6\u624d\u5b8c\u6210\u521d\u59cb\u5316\u5de5\u4f5c\u3002\u907f\u514d\u4e86\u5185\u5b58\u6d6a\u8d39\u548c\u542f\u52a8\u7f13\u6162\u3002\u4f46\u662f\u6b64\u65f6\u53ef\u80fd\u5b58\u5728\u591a\u7ebf\u7a0b\u7ade\u6001\u73af\u5883&#xff0c;\u5982\u4e0d\u52a0\u9501\u9650\u5236\u4f1a\u5bfc\u81f4\u91cd\u590d\u6784\u9020\u6216\u6784\u9020\u4e0d\u5b8c\u5168\u95ee\u9898\u3002<\/p>\n<h5>\u53cc\u91cd\u68c0\u67e5\u9501 \u914d\u5408 volatile\u5173\u952e\u5b57&#xff08;DCL\u7248\u672c&#xff09;<\/h5>\n<p>\u5355\u4f8b\u7c7b\u5b9e\u73b0\u7684\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Singleton<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token keyword\">private<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">volatile<\/span> <span class=\"token class-name\">Singleton<\/span> instance<span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token comment\">\/\/\u6784\u9020\u51fd\u6570<\/span><br \/>\n  <span class=\"token keyword\">private<\/span> <span class=\"token class-name\">Singleton<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/*<br \/>\n     \u9632\u6b62\u6709\u4eba\u901a\u8fc7\u53cd\u5c04\u8c03\u7528\u6784\u9020\u51fd\u6570<br \/>\n     \u5373\u4fbf\u4f60\u901a\u8fc7\u53cd\u5c04\u7ed5\u8fc7\u4e86 private&#xff0c;\u4f46\u662f\u53ea\u8981 instance \u5df2\u7ecf\u88ab\u8d4b\u503c&#xff0c;\u7b2c\u4e8c\u6b21\u5c1d\u8bd5\u8fdb\u5165\u6784\u9020\u51fd\u6570\u90fd\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<br \/>\n    *\/<\/span><br \/>\n    <span class=\"token keyword\">synchronized<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Singleton<\/span><span class=\"token punctuation\">.<\/span><span class=\"token keyword\">class<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n      <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>instance <span class=\"token operator\">!&#061;<\/span> <span class=\"token keyword\">null<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">throw<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">RuntimeException<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u5355\u4f8b\u5bf9\u8c61\u5df2\u7ecf\u5b9e\u4f8b\u5316\u4e86&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n      <span class=\"token punctuation\">}<\/span><br \/>\n      <span class=\"token comment\">\/\/\u5c06\u521d\u59cb\u5316\u5bf9\u8c61\u8d4b\u503c\u7ed9\u9759\u6001\u53d8\u91cf<\/span><br \/>\n      <span class=\"token class-name\">Singleton<\/span><span class=\"token punctuation\">.<\/span>instance <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">this<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><br \/>\n  <span class=\"token comment\">\/\/getInstance()\u65b9\u6cd5<\/span><br \/>\n  <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token class-name\">Singleton<\/span> <span class=\"token function\">getInstance<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>instance <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token keyword\">null<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><span class=\"token comment\">\/\/\u63d0\u9ad8\u6027\u80fd<\/span><br \/>\n      <span class=\"token comment\">\/*<br \/>\n      \u6307\u5411\u7684\u662f\u8be5\u7c7b\u5728 JVM \u4e2d\u7684 Class \u5bf9\u8c61\u3002<br \/>\n      \u7531\u4e8e\u5728\u4e00\u4e2a\u7c7b\u52a0\u8f7d\u5668\u4e0b&#xff0c;\u6bcf\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a Class \u5bf9\u8c61&#xff0c;\u56e0\u6b64 \u7c7b\u9501\u662f\u5168\u5c40\u552f\u4e00\u7684<br \/>\n      \u4f7f\u6709\u6210\u5343\u4e0a\u4e07\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee&#xff0c;\u53ea\u8981\u5b83\u4eec\u6267\u884c\u5230\u8fd9\u4e00\u884c&#xff0c;\u90fd\u5fc5\u987b\u7ade\u4e89\u8fd9\u628a\u552f\u4e00\u7684\u201c\u7c7b\u94a5\u5319\u201d\u3002<br \/>\n      *\/<\/span><br \/>\n      <span class=\"token keyword\">synchronized<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Singleton<\/span><span class=\"token punctuation\">.<\/span><span class=\"token keyword\">class<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><span class=\"token comment\">\/\/\u7c7b\u9501&#xff0c;\u4fdd\u8bc1\u5e76\u53d1\u5b89\u5168<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>instance <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token keyword\">null<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><br \/>\n          instance <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">Singleton<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n      <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> instance<span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u5148\u89e3\u91ca\u4e00\u4e0b\u8fd9\u4e2a\u7c7b<\/p>\n<ul>\n<li>volatile\u5173\u952e\u5b57,volatile \u63d0\u4f9b\u4e86\u4e24\u4e2a\u6838\u5fc3\u7279\u6027&#xff1a;\u53ef\u89c1\u6027 \u548c \u7981\u6b62\u6307\u4ee4\u91cd\u6392&#xff0c;\u8fd9\u6b63\u662f\u8981\u4f7f\u7528\u8fd9\u4e2a\u5173\u952e\u5b57\u7684\u539f\u56e0\n<ul>\n<li>\u53ef\u89c1\u6027&#xff1a;\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u5de5\u4f5c\u5185\u5b58\u3002\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u4fee\u6539\u4e86\u67d0\u4e2a\u53d8\u91cf&#xff0c;\u5176\u4ed6\u7ebf\u7a0b\u4e0d\u4e00\u5b9a\u4f1a\u7acb\u523b\u770b\u5230\u3002\n<ul>\n<li>\u6709\u4e86\u8fd9\u4e2a\u5173\u952e\u5b57&#xff0c;\u4f8b\u5982\u73b0\u5728\u6709\u4e24\u4e2a\u7ebf\u7a0bA\u3001B&#xff0c;\u4e00\u65e6\u7ebf\u7a0b A \u4fee\u6539\u4e86 instance&#xff0c;JVM \u4f1a\u5f3a\u5236\u8981\u6c42\u8be5\u503c\u7acb\u5373\u5237\u5165\u4e3b\u5185\u5b58&#xff0c;\u5e76\u4e14\u8ba9\u5176\u4ed6\u7ebf\u7a0b\u7f13\u5b58\u7684\u8be5\u53d8\u91cf\u5931\u6548\u3002\u8fd9\u6837\u5c31\u4e0d\u4f1a\u51fa\u73b0\u5f53\u7ebf\u7a0b A \u4fee\u6539\u4e86 instance&#xff0c;\u7ebf\u7a0b B \u53ef\u80fd\u8fd8\u5728\u8bfb\u81ea\u5df1\u7f13\u5b58\u91cc\u7684\u65e7\u503c null&#xff0c;\u5bfc\u81f4\u7ebf\u7a0b B \u53c8\u53bb\u521b\u5efa\u4e86\u4e00\u4e2a\u5b9e\u4f8b\u7684\u60c5\u51b5\u4e86<\/li>\n<\/ul>\n<\/li>\n<li>\u7981\u6b62\u6307\u4ee4\u91cd\u6392&#xff1a;\u770b\u4e00\u53e5\u4ee3\u7801\u7684\u5b9e\u73b0&#xff0c;\u5982instance &#061; new Singleton();\u8fd9\u884c\u4ee3\u7801\u3002\u867d\u7136\u5728 Java \u91cc\u8fd9\u662f\u4e00\u884c\u4ee3\u7801&#xff0c;\u4f46\u5728 JVM \u5e95\u5c42\u5b83\u5b9e\u9645\u4e0a\u5206\u6210\u4e86 3 \u6b65&#xff1a;\n<ul>\n<li>\u5206\u914d\u5185\u5b58\u7a7a\u95f4\u3001\u521d\u59cb\u5316\u5bf9\u8c61\u3001\u5c06\u5f15\u7528\u6307\u5411\u5185\u5b58\u7a7a\u95f4<\/li>\n<li>\u7f16\u8bd1\u5668\u6216 CPU \u4e3a\u4e86\u4f18\u5316\u6027\u80fd&#xff0c;\u53ef\u80fd\u4f1a\u628a\u987a\u5e8f\u53d8\u6210 1 -&gt; 3 -&gt; 2\u3002<\/li>\n<li>\u5982\u679c\u6267\u884c\u52303\u4e86&#xff0c;\u8fd8\u6ca1\u6709\u6267\u884c2&#xff0c;\u4f46\u662f\u6b64\u65f6\u5f15\u7528instance \u5df2\u7ecf\u6307\u5411\u4e86\u5206\u914d\u7684\u90a3\u5757\u5185\u5b58\u7a7a\u95f4\u4e86&#xff01;\u6b64\u65f6\u5c31\u4f1a\u51fa\u95ee\u9898&#xff0c;\u5982\u679c\u6b64\u65f6\u521a\u597d\u6709\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u5230getInstance() \u7684\u7b2c\u4e00\u4e2a\u5224\u65ad if (instance &#061;&#061; null)&#xff0c;\u53d1\u73b0\u5b83\u4e0d\u4e3a null&#xff0c;\u4e8e\u662f\u76f4\u63a5\u8fd4\u56de\u4e86 instance\u3002\u6b64\u65f6\u7684instance\u662f\u4e00\u4e2a\u6ca1\u6709\u6267\u884c\u6b65\u9aa42\u7684&#034;\u534a\u6210\u54c1&#034;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>synchronized (Singleton.class)&#xff0c;\u7c7b\u9501 -&gt; \u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\n<ul>\n<li>\u7c7b\u9501\u662f\u5168\u5c40\u552f\u4e00\u7684&#xff0c;\u5f71\u54cd\u8bbf\u95ee\u8be5\u7c7b\u6240\u6709\u5b9e\u4f8b&#xff0c;\u4e0d\u540c\u4e8e\u5bf9\u8c61\u9501synchronized(this)&#xff0c;\u5bf9\u8c61\u9501\u53ea\u5f71\u54cd\u8bbf\u95ee\u540c\u4e00\u4e2a\u5b9e\u4f8b\u7684\u7ebf\u7a0b<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<p>\u518d\u770b\u6d4b\u8bd5\u7c7b&#xff0c;\u901a\u8fc7\u6d4b\u8bd5\u7c7b\u6765\u660e\u767d\u5355\u4f8b\u7c7b\u7684\u6d41\u7a0b\u662f\u4ec0\u4e48\u6837\u5b50\u7684<\/p>\n<p>\u5148\u770b\u4e00\u4e0b&#034;\u5148\u53cd\u5c04\u521b\u5efa\u5bf9\u8c61&#xff0c;\u7136\u540e\u83b7\u53d6\u5355\u4f8b\u5bf9\u8c61&#034;\u8fd9\u79cd\u60c5\u51b5<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">ReflectInjection<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token annotation punctuation\">&#064;SneakyThrows<\/span><span class=\"token comment\">\/\/\u7528\u4e8e\u629b\u5f02\u5e38\u7684\u6ce8\u89e3<\/span><br \/>\n  <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> args<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">throws<\/span> <span class=\"token class-name\">Exception<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/\u83b7\u53d6\u5230\u7c7b\u5b9a\u4e49<\/span><br \/>\n    <span class=\"token class-name\">Class<\/span> x <span class=\"token operator\">&#061;<\/span> <span class=\"token class-name\">Singleton<\/span><span class=\"token punctuation\">.<\/span><span class=\"token keyword\">class<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/\u83b7\u53d6\u6784\u9020\u5668<\/span><br \/>\n    <span class=\"token class-name\">Constructor<\/span> constructor <span class=\"token operator\">&#061;<\/span> x<span class=\"token punctuation\">.<\/span><span class=\"token function\">getDeclaredConstructors<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/\u8bbe\u7f6e\u53ef\u4ee5\u8bbf\u95ee<\/span><br \/>\n    constructor<span class=\"token punctuation\">.<\/span><span class=\"token function\">setAccessible<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">true<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token class-name\">Singleton<\/span> o <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Singleton<\/span><span class=\"token punctuation\">)<\/span> constructor<span class=\"token punctuation\">.<\/span><span class=\"token function\">newInstance<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/\u83b7\u53d6\u5355\u4f8b<\/span><br \/>\n    <span class=\"token class-name\">Singleton<\/span> instance <span class=\"token operator\">&#061;<\/span> <span class=\"token class-name\">Singleton<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">getInstance<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/\u6bd4\u8f83\u5355\u4f8b\u4e0e\u521d\u59cb\u5316\u7684\u5b9e\u4f8b<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>instance <span class=\"token operator\">&#061;&#061;<\/span> o<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n      <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u76f8\u540c\u7684\u5b9e\u4f8b&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n      <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u4e0d\u540c\u7684\u5b9e\u4f8b&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u6b64\u65f6\u7ed3\u679c\u6b63\u5e38\u8f93\u51fa<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226154310-69a06a0eb8e67.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u89e3\u91ca<\/p>\n<ul>\n<li>\u53cd\u5c04\u521b\u5efa\u5bf9\u8c61newInstance(),\u8fdb\u5165 Singleton \u7684\u6784\u9020\u51fd\u6570<\/li>\n<li>\u78b0\u5230synchronized(Singleton.class) ,\u53d1\u73b0\u7c7b\u9501\u672a\u88ab\u62ff\u8d70&#xff0c;\u6267\u884c instance &#061; this\u3002<\/li>\n<li>\u53cd\u5c04\u51fa\u6765\u7684\u5bf9\u8c61 o \u88ab\u8d4b\u503c\u7ed9\u4e86\u9759\u6001\u53d8\u91cf instance\u3002<\/li>\n<li>\u540e\u7eed\u83b7\u53d6\u5355\u4f8b\u7684\u65f6\u5019&#xff0c;\u6267\u884cgetInstance&#xff0c;\u6b64\u65f6\u53d1\u73b0instance\u4e0d\u662fnull,\u76f4\u63a5\u8fd4\u56de\u53cd\u5c04\u521b\u5efa\u7684\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p>\u518d\u770b\u4e00\u4e0b\u5148\u83b7\u53d6\u5355\u4f8b\u5bf9\u8c61&#xff0c;\u7136\u540e\u53cd\u5c04\u521b\u5efa\u5bf9\u8c61\u8fd9\u79cd\u60c5\u51b5<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">ReflectInjection<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token annotation punctuation\">&#064;SneakyThrows<\/span><br \/>\n  <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> args<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">throws<\/span> <span class=\"token class-name\">Exception<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/\u83b7\u53d6\u5355\u4f8b<\/span><br \/>\n    <span class=\"token class-name\">Singleton<\/span> instance <span class=\"token operator\">&#061;<\/span> <span class=\"token class-name\">Singleton<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">getInstance<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u83b7\u53d6\u5230\u7c7b\u5b9a\u4e49<\/span><br \/>\n    <span class=\"token class-name\">Class<\/span> x <span class=\"token operator\">&#061;<\/span> <span class=\"token class-name\">Singleton<\/span><span class=\"token punctuation\">.<\/span><span class=\"token keyword\">class<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/\u83b7\u53d6\u6784\u9020\u5668<\/span><br \/>\n    <span class=\"token class-name\">Constructor<\/span> constructor <span class=\"token operator\">&#061;<\/span> x<span class=\"token punctuation\">.<\/span><span class=\"token function\">getDeclaredConstructors<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/\u8bbe\u7f6e\u53ef\u4ee5\u8bbf\u95ee<\/span><br \/>\n    constructor<span class=\"token punctuation\">.<\/span><span class=\"token function\">setAccessible<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">true<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token class-name\">Singleton<\/span> o <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Singleton<\/span><span class=\"token punctuation\">)<\/span> constructor<span class=\"token punctuation\">.<\/span><span class=\"token function\">newInstance<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u6bd4\u8f83\u5355\u4f8b\u4e0e\u521d\u59cb\u5316\u7684\u5b9e\u4f8b<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>instance <span class=\"token operator\">&#061;&#061;<\/span> o<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n      <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u76f8\u540c\u7684\u5b9e\u4f8b&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n      <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u4e0d\u540c\u7684\u5b9e\u4f8b&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226154310-69a06a0ee2711.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u89e3\u91ca<\/p>\n<ul>\n<li>\u83b7\u53d6\u5355\u4f8b (getInstance)&#xff0c;\u6b64\u65f6\u5224\u65adinstance\u662fnull&#xff0c;\u7136\u540e\u83b7\u53d6\u5230\u7c7b\u9501\u3002<\/li>\n<li>\u6267\u884c new Singleton()\u3002\u6b64\u65f6\u4f1a\u5d4c\u5957\u8c03\u7528\u6784\u9020\u51fd\u6570\u3002<\/li>\n<li>\u6267\u884c\u6784\u9020\u51fd\u6570\u5185\u7684 synchronized\u3002\u6ce8\u610f&#xff0c;\u6b64\u65f6\u662f\u540c\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u7531\u4e8e Java \u7684\u9501\u662f\u53ef\u91cd\u5165\u7684&#xff0c;\u5b83\u80fd\u76f4\u63a5\u8fdb\u5165\u3002<\/li>\n<li>\u7136\u540e\u8fdb\u884c\u8d4b\u503c&#xff1a;instance &#061; this&#xff08;\u8fd9\u662f\u7b2c\u4e00\u6b21\u6b63\u5f0f\u8d4b\u503c&#xff09;<\/li>\n<li>\u8fd4\u56de&#xff1a;getInstance \u91cc\u7684 instance &#061; new Singleton() \u518d\u6b21\u786e\u8ba4\u8d4b\u503c\u3002<\/li>\n<li>\u7136\u540e\u53cd\u5c04\u521b\u5efa (newInstance)&#xff1a;\u53cd\u5c04\u5f3a\u884c\u8fdb\u5165\u6784\u9020\u51fd\u6570\u3002<\/li>\n<li>\u6267\u884c\u6784\u9020\u51fd\u6570\u5185\u7684 synchronized&#xff0c;\u53cd\u5c04\u7ebf\u7a0b\u62ff\u5230\u9501\u3002\u6267\u884c\u5224\u65ad&#xff0c;\u4f46\u662f\u6b64\u65f6 instance \u5df2\u7ecf\u4e0d\u4e3a null \u4e86&#xff0c;\u89e6\u53d1 throw new RuntimeException\u3002<\/li>\n<\/ul>\n<hr \/>\n<p>\u6b64\u65f6\u5e94\u8be5\u80fd\u6e05\u695a\u4e86\u5355\u4f8b\u7c7b\u662f\u600e\u6837\u7684\u5de5\u4f5c\u6d41\u7a0b&#xff0c;\u73b0\u5728\u6839\u636e\u4e0a\u9762\u7684\u6d41\u7a0b\u89e3\u91ca&#xff0c;\u8fdb\u884c\u4e24\u4e2a\u601d\u8003&#xff1a;<\/p>\n<li>\n<p>getInstance\u65b9\u6cd5\u4e3a\u4ec0\u4e48\u6709\u4e24\u6b21\u5224\u7a7a&#xff0c;\u4ed6\u4eec\u5206\u522b\u7684\u4f5c\u7528\u662f\u4ec0\u4e48&#xff1f;<\/p>\n<ul>\n<li>\n<p>\u5916\u5c42\u5224\u7a7a&#xff1a;\u8fc7\u6ee4\u5df2\u521b\u5efa\u5b9e\u4f8b\u7684\u8bf7\u6c42&#xff0c;\u63d0\u9ad8\u6027\u80fd\u3002\u5f53\u5355\u4f8b\u5bf9\u8c61\u5df2\u7ecf\u521b\u5efa\u597d\u540e&#xff0c;\u540e\u7eed\u6240\u6709\u7684\u8c03\u7528\u90fd\u4e0d\u9700\u8981\u518d\u8fdb\u5165 synchronized \u5757\u4e86\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u5347\u6027\u80fd<\/p>\n<\/li>\n<li>\n<p>\u5185\u5c42\u5224\u7a7a&#xff1a;\u9632\u6b62\u5728\u591a\u7ebf\u7a0b\u5e76\u53d1\u4e0b\u91cd\u590d\u521b\u5efa\u5bf9\u8c61\u3002\u7c7b\u9501\u4fdd\u8bc1\u4e86\u5168 JVM \u8303\u56f4\u5185&#xff0c;\u521b\u5efa\u52a8\u4f5c\u662f\u5355\u7ebf\u7a0b\u6267\u884c\u7684\u3002<\/p>\n<ul>\n<li>\u4f8b\u5982\u7ebf\u7a0b A \u548c \u7ebf\u7a0b B \u540c\u65f6\u6267\u884c&#xff0c;\u90fd\u53d1\u73b0\u5916\u5c42 instance \u4e3a null\u3002\u7ebf\u7a0b A \u62a2\u5230\u4e86\u9501&#xff0c;\u8fdb\u53bb new \u4e86\u4e00\u4e2a\u5bf9\u8c61\u3002\u7ebf\u7a0b A \u91ca\u653e\u9501\u79bb\u5f00\u3002\u7136\u540e\u6b64\u65f6\u7ebf\u7a0bB\u62a2\u5230\u4e86\u9501&#xff0c;\u5982\u679c\u6ca1\u6709\u8fd9\u5c42\u5185\u5c42\u5224\u7a7a&#xff0c;\u7ebf\u7a0b B\u4f1a\u518dnew\u4e00\u4e2a\u5bf9\u8c61,\u8fd9\u6837\u4f1a\u4f7f\u5f97\u5355\u4f8b\u5931\u6548\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6784\u9020\u51fd\u6570\u5224\u7a7a\u7684\u4f5c\u7528\u662f\u4ec0\u4e48&#xff1f;<\/p>\n<ul>\n<li>\u4f5c\u7528\u662f\u624b\u52a8\u903b\u8f91\u8fdb\u884c\u53cd\u5c04\u62e6\u622a&#xff0c;\u4e0d\u540c\u4e8e\u679a\u4e3e\u7684 JVM \u81ea\u52a8\u62e6\u622a&#xff0c;DCL \u6a21\u5f0f\u9700\u8981\u5728\u6784\u9020\u51fd\u6570\u4e2d\u624b\u52a8\u52a0\u5165\u9632\u5fa1\u4ee3\u7801\u3002\u5f53\u6211\u4eec\u5df2\u7ecf\u5148\u4f7f\u7528getInstance\u521b\u5efa\u4e86\u5355\u4f8b\u5bf9\u8c61\u4e4b\u540e&#xff0c;\u5982\u679c\u540e\u7eed\u518d\u6709\u4eba\u4f7f\u7528\u53cd\u5c04\u5c1d\u8bd5\u4f7f\u7528\u6784\u9020\u51fd\u6570\u521b\u5efa\u5bf9\u8c61&#xff0c;\u5c31\u4f1a\u88ab\u5224\u7a7a\u903b\u8f91\u62e6\u622a&#xff0c;\u629b\u51fa\u5f02\u5e38&#xff01;\u907f\u514d\u4e86\u975e\u6cd5\u5bf9\u8c61\u7684\u4ea7\u751f&#xff01;<\/li>\n<\/ul>\n<\/li>\n<h5>\u9759\u6001\u5185\u90e8\u7c7b\u5355\u4f8b&#xff08; Holder \u6a21\u5f0f&#xff09;<\/h5>\n<p>\u8fd9\u662f\u4e00\u79cd\u5229\u7528 JVM \u7c7b\u52a0\u8f7d\u673a\u5236 \u6765\u5de7\u5999\u5b9e\u73b0\u61d2\u6c49\u6a21\u5f0f\u7684\u65b9\u6848\u3002<\/p>\n<p>\u5728\u4fdd\u8bc1\u5ef6\u8fdf\u52a0\u8f7d\u7684\u540c\u65f6&#xff0c;\u5229\u7528 JVM \u5e95\u5c42\u673a\u5236\u5b9e\u73b0\u4e86\u9ad8\u6027\u80fd\u7684\u7ebf\u7a0b\u5b89\u5168&#xff0c;\u907f\u514d\u4e86\u4e0a\u4e00\u79cd\u65b9\u6cd5\u4e2d\u590d\u6742\u7684\u9501\u903b\u8f91\u3002<\/p>\n<p>\u4e00\u53e5\u8bdd&#xff1a;\u4f7f\u7528\u9759\u6001\u5185\u90e8\u7c7b\u521b\u5efa\u5355\u4f8b\u5bf9\u8c61<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Singleton<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token comment\">\/\/ \u6784\u9020\u5668<\/span><br \/>\n  <span class=\"token keyword\">private<\/span> <span class=\"token class-name\">Singleton<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/*<br \/>\n     * \u8ddfDCL\u7248\u672c\u4e00\u6837\u7684\u9632\u5fa1\u53cd\u5c04\u653b\u51fb\u624b\u6bb5<br \/>\n     *\/<\/span><br \/>\n    <span class=\"token keyword\">synchronized<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Singleton<\/span><span class=\"token punctuation\">.<\/span><span class=\"token keyword\">class<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n      <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">SingletonHandler<\/span><span class=\"token punctuation\">.<\/span>singleton <span class=\"token operator\">!&#061;<\/span> <span class=\"token keyword\">null<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">throw<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">RuntimeException<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u5355\u4f8b\u5bf9\u8c61\u5df2\u7ecf\u5b9e\u4f8b\u5316\u4e86&#xff0c;\u975e\u6cd5\u8bbf\u95ee&#xff01;&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n      <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><\/p>\n<p>  <span class=\"token comment\">\/\/ \u5185\u90e8\u7c7b\u91cc\u9762\u521b\u5efa\u4e86\u4e00\u4e2a\u9759\u6001\u7684\u5bf9\u8c61<\/span><br \/>\n  <span class=\"token keyword\">private<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">SingletonHandler<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">private<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">final<\/span> <span class=\"token class-name\">Singleton<\/span> singleton <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">Singleton<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><\/p>\n<p>  <span class=\"token comment\">\/\/ \u5bf9\u5916\u65b9\u6cd5<\/span><br \/>\n  <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token class-name\">Singleton<\/span> <span class=\"token function\">getInstance<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token class-name\">SingletonHandler<\/span><span class=\"token punctuation\">.<\/span>singleton<span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u89e3\u91ca<\/p>\n<ul>\n<li>\n<p>\u5728\u5916\u90e8\u7c7b Singleton \u4e2d\u5b9a\u4e49\u4e00\u4e2a\u79c1\u6709\u7684\u9759\u6001\u5185\u90e8\u7c7b SingletonHandler\u3002\u53ea\u6709\u5728\u663e\u5f0f\u8c03\u7528 getInstance() \u65b9\u6cd5\u65f6&#xff0c;\u624d\u4f1a\u89e6\u53d1\u5185\u90e8\u7c7b\u7684\u52a0\u8f7d&#xff0c;\u8fdb\u800c\u521d\u59cb\u5316\u5176\u9759\u6001\u5e38\u91cf singleton\u3002<\/p>\n<p>\u4f60\u8c03\u7528 Singleton.class \u6216\u8005\u8bbf\u95ee\u5916\u90e8\u7c7b\u7684\u5176\u4ed6\u9759\u6001\u53d8\u91cf\u65f6&#xff0c;Singleton \u7c7b\u4f1a\u88ab\u52a0\u8f7d&#xff0c;\u4f46 SingletonHandler \u5e76\u4e0d\u4f1a\u88ab\u52a0\u8f7d\u3002<\/p>\n<p>\u53ea\u6709\u5f53\u4f60\u7b2c\u4e00\u6b21\u8c03\u7528 Singleton.getInstance() \u65f6&#xff0c;\u4ee3\u7801\u6267\u884c\u5230\u4e86 return SingletonHandler.singleton;\u3002\u6b64\u65f6\u624d\u4f1a\u89e6\u53d1 SingletonHandler \u7684\u7c7b\u52a0\u8f7d\u7a0b\u5e8f\u3002<\/p>\n<\/li>\n<li>\n<p>\u5229\u7528 JVM \u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168:<\/p>\n<ul>\n<li>\u4e0e\u679a\u4e3e\u5355\u4f8b\u7c7b\u4f3c&#xff0c;JVM \u5728\u6267\u884c\u8fd9\u4e2a\u5185\u90e8\u7c7b\u7684\u521d\u59cb\u5316\u65f6\u4f1a\u52a0\u9501\u3002<\/li>\n<li>\u5982\u679c\u6709\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5c1d\u8bd5\u89e6\u53d1\u5185\u90e8\u7c7b\u7684\u52a0\u8f7d&#xff0c;JVM \u4f1a\u786e\u4fdd\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53bb\u6267\u884c new Singleton()&#xff0c;\u5176\u4ed6\u7ebf\u7a0b\u5219\u963b\u585e\u7b49\u5f85\u76f4\u5230\u521d\u59cb\u5316\u5b8c\u6210\u3002\u8fd9\u79cd\u7ebf\u7a0b\u5b89\u5168\u6027\u662f\u7531 JVM \u539f\u751f\u4fdd\u969c\u7684&#xff0c;\u6bd4 DCL \u7684\u624b\u52a8\u52a0\u9501\u6027\u80fd\u66f4\u9ad8\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u624b\u52a8\u8fdb\u884c\u53cd\u5c04\u62e6\u622a<\/p>\n<ul>\n<li>\u4e0e DCL \u6a21\u5f0f\u4e00\u6837&#xff0c;\u9759\u6001\u5185\u90e8\u7c7b\u672c\u8d28\u4e0a\u8fd8\u662f\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521b\u5efa\u5bf9\u8c61\u7684\u3002<\/li>\n<li>\u6240\u4ee5\u4e3a\u4e86\u907f\u514d\u4f7f\u7528\u53cd\u5c04\u6765\u5229\u7528\u6784\u9020\u51fd\u6570\u521b\u5efa\u5bf9\u8c61&#xff0c;\u9700\u8981\u5728\u6784\u9020\u51fd\u6570\u8fdb\u884c\u5224\u7a7a\u903b\u8f91<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\u4f7f\u7528\u73af\u5883<\/h3>\n<p>\u5355\u4f8b\u6a21\u5f0f\u4f5c\u4e3a\u4e00\u79cd\u76ee\u6807\u660e\u786e\u3001\u7ed3\u6784\u7b80\u5355\u3001\u7406\u89e3\u5bb9\u6613\u7684\u8bbe\u8ba1\u6a21\u5f0f&#xff0c;\u5728\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u4f7f\u7528\u9891\u7387\u76f8\u5f53\u9ad8&#xff0c;\u5728\u5f88\u591a\u5e94\u7528\u8f6f\u4ef6\u548c\u6846\u67b6\u4e2d\u90fd\u5f97\u4ee5\u5e7f\u6cdb\u5e94\u7528\u3002<\/p>\n<h4>\u4e3b\u8981\u4f18\u70b9<\/h4>\n<ul>\n<li>\u5355\u4f8b\u6a21\u5f0f\u63d0\u4f9b\u4e86\u5bf9\u552f\u4e00\u5b9e\u4f8b\u7684\u53d7\u63a7\u8bbf\u95ee\u3002\u56e0\u4e3a\u5355\u4f8b\u7c7b\u5c01\u88c5\u4e86\u5b83\u7684\u552f\u4e00\u5b9e\u4f8b&#xff0c;\u6240\u4ee5\u5b83\u53ef\u4ee5\u4e25\u683c\u63a7\u5236\u5ba2\u6237\u600e\u6837\u4ee5\u53ca\u4f55\u65f6\u8bbf\u95ee\u5b83\u3002<\/li>\n<li>\u7531\u4e8e\u5728\u7cfb\u7edf\u5185\u5b58\u4e2d\u53ea\u5b58\u5728\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u56e0\u6b64\u53ef\u4ee5\u8282\u7ea6\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u5bf9\u4e8e\u4e00\u4e9b\u9700\u8981\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u7684\u5bf9\u8c61\u5355\u4f8b\u6a21\u5f0f\u65e0\u7591\u53ef\u4ee5\u63d0\u9ad8\u7cfb\u7edf\u7684\u6027\u80fd\u3002<\/li>\n<li>\u5141\u8bb8\u53ef\u53d8\u6570\u76ee\u7684\u5b9e\u4f8b&#xff08;\u591a\u4f8b\u6a21\u5f0f&#xff09;\u3002\u57fa\u4e8e\u5355\u4f8b\u6a21\u5f0f\u6211\u4eec\u53ef\u4ee5\u8fdb\u884c\u6269\u5c55&#xff0c;\u4f7f\u7528\u4e0e\u5355\u4f8b\u63a7\u5236\u76f8\u4f3c\u7684\u65b9\u6cd5\u6765\u83b7\u5f97\u6307\u5b9a\u4e2a\u6570\u7684\u5bf9\u8c61\u5b9e\u4f8b&#xff0c;\u65e2\u8282\u7701\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u53c8\u89e3\u51b3\u4e86\u5355\u4f8b\u5bf9\u8c61\u5171\u4eab\u8fc7\u591a\u6709\u635f\u6027\u80fd\u7684\u95ee\u9898\u3002<\/li>\n<\/ul>\n<h4>\u4e3b\u8981\u7f3a\u70b9<\/h4>\n<ul>\n<li>\u7531\u4e8e\u5355\u4f8b\u6a21\u5f0f\u4e2d\u6ca1\u6709\u62bd\u8c61\u5c42&#xff0c;\u56e0\u6b64\u5355\u4f8b\u7c7b\u7684\u6269\u5c55\u6709\u5f88\u5927\u7684\u56f0\u96be\u3002<\/li>\n<li>\u5355\u4f8b\u7c7b\u7684\u804c\u8d23\u8fc7\u91cd&#xff0c;\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u8fdd\u80cc\u4e86\u201c\u5355\u4e00\u804c\u8d23\u539f\u5219\u201d\u3002\u56e0\u4e3a\u5355\u4f8b\u7c7b\u65e2\u5145\u5f53\u4e86\u5de5\u5382\u89d2\u8272&#xff0c;\u63d0\u4f9b\u4e86\u5de5\u5382\u65b9\u6cd5&#xff0c;\u540c\u65f6\u53c8\u5145\u5f53\u4e86\u4ea7\u54c1\u89d2\u8272&#xff0c;\u5305\u542b\u4e00\u4e9b\u4e1a\u52a1\u65b9\u6cd5&#xff0c;\u5c06\u4ea7\u54c1\u7684\u521b\u5efa\u548c\u4ea7\u54c1\u7684\u672c\u8eab\u7684\u529f\u80fd\u878d\u5408\u5230\u4e00\u8d77\u3002<\/li>\n<li>\u73b0\u5728\u5f88\u591a\u9762\u5411\u5bf9\u8c61\u8bed\u8a00(\u5982Java\u3001C#)\u7684\u8fd0\u884c\u73af\u5883\u90fd\u63d0\u4f9b\u4e86\u81ea\u52a8\u5783\u573e\u56de\u6536\u7684\u6280\u672f&#xff0c;\u56e0\u6b64&#xff0c;\u5982\u679c\u5b9e\u4f8b\u5316\u7684\u5171\u4eab\u5bf9\u8c61\u957f\u65f6\u95f4\u4e0d\u88ab\u5229\u7528&#xff0c;\u7cfb\u7edf\u4f1a\u8ba4\u4e3a\u5b83\u662f\u5783\u573e&#xff0c;\u4f1a\u81ea\u52a8\u9500\u6bc1\u5e76\u56de\u6536\u8d44\u6e90&#xff0c;\u4e0b\u6b21\u5229\u7528\u65f6\u53c8\u5c06\u91cd\u65b0\u5b9e\u4f8b\u5316&#xff0c;\u8fd9\u5c06\u5bfc\u81f4\u5171\u4eab\u7684\u5355\u4f8b\u5bf9\u8c61\u72b6\u6001\u7684\u4e22\u5931\u3002<\/li>\n<\/ul>\n<h4>\u9002\u7528\u73af\u5883<\/h4>\n<p>\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f:<\/p>\n<ul>\n<li>\u7cfb\u7edf\u53ea\u9700\u8981\u4e00\u4e2a\u5b9e\u4f8b\u5bf9\u8c61&#xff0c;\u5982\u7cfb\u7edf\u8981\u6c42\u63d0\u4f9b\u4e00\u4e2a\u552f\u4e00\u7684\u5e8f\u5217\u53f7\u751f\u6210\u5668\u6216\u8d44\u6e90\u7ba1\u7406\u5668&#xff0c;\u6216\u8005\u9700\u8981\u8003\u8651\u8d44\u6e90\u6d88\u8017\u592a\u5927\u800c\u53ea\u5141\u8bb8\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u3002<\/li>\n<li>\u5ba2\u6237\u8c03\u7528\u7c7b\u7684\u5355\u4e2a\u5b9e\u4f8b\u53ea\u5141\u8bb8\u4f7f\u7528\u4e00\u4e2a\u516c\u5171\u8bbf\u95ee\u70b9&#xff0c;\u9664\u4e86\u8be5\u516c\u5171\u8bbf\u95ee\u70b9&#xff0c;\u4e0d\u80fd\u901a\u8fc7\u5176\u4ed6\u9014\u5f84\u8bbf\u95ee\u8be5\u5b9e\u4f8b\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u5e2e\u52a9&#xff0c;\u6b22\u8fce<font color=\"red\">\u70b9\u8d5e\u3001\u8bc4\u8bba\u3001\u5173\u6ce8\u3001\u6536\u85cf<\/font>\u3002\u4f60\u4eec\u7684\u652f\u6301\u662f\u6211\u524d\u8fdb\u7684\u52a8\u529b&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tip&#xff1a;\u672c\u7bc7\u7684\u4ee3\u7801\u5b9e\u73b0\u90fd\u662f\u57fa\u4e8eJava\u7684\u5b9e\u73b0\u3002<br \/>\n\u800c\u4e14\u7531\u4e8e\u4e0d\u540c\u8bed\u8a00\u7684\u7279\u6027\u4e0d\u540c\u3002\u4f8b\u5982c\u8ddfjava\u7684\u5185\u5b58\u7ba1\u7406\u662f\u4e0d\u4e00\u6837\u7684&#xff0c;\u8bed\u6cd5\u4e0a\u4e5f\u7565\u6709\u5dee\u522b&#xff0c;\u6240\u4ee5\u5355\u4f8b\u6a21\u5f0f\u7684\u5b9e\u73b0\u4e5f\u6709\u5dee\u522b\u3002\u4f8b\u5982C\u7684\u997f\u6c49\u6a21\u5f0f\u53ef\u4ee5\u6709\\&#8221;\u975e\u5c40\u90e8\u9759\u6001\u5b9e\u73b0\\&#8221;\u3001\\&#8221;\u5c40\u90e8\u9759\u6001\u5b9e\u73b0\\&#8221;\u7b49\u7b49&#xff0c;\u5982\u679c\u60f3\u4e86\u89e3C\u7248\u672c\u7684\u5355\u4f8b\u6a21\u5f0f\u81ea\u884c\u767e\u5ea6\u5373\u53ef\u6982\u8ff0<br \/>\n\u5bf9\u4e8e\u4e00\u4e2a\u8f6f\u4ef6\u7cfb\u7edf\u7684\u67d0\u4e9b\u7c7b\u800c\u8a00&#xff0c;\u6211\u4eec\u65e0\u987b\u521b\u5efa\u591a\u4e2a\u5b9e\u4f8b\u3002\u4e3a\u4e86\u8282\u7ea6\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u6709\u65f6\u9700\u8981\u786e\u4fdd\u7cfb\u7edf\u4e2d\u67d0\u4e2a\u7c7b\u53ea\u6709\u552f\u4e00\u4e00\u4e2a\u5b9e\u4f8b&#xff0c;\u5f53<\/p>\n","protected":false},"author":2,"featured_media":78295,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2246,2874],"topic":[],"class_list":["post-78300","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-2246","tag-2874"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010\u4ece0\u5f00\u59cb\u5b66\u8bbe\u8ba1\u6a21\u5f0f-4| \u5355\u4f8b\u6a21\u5f0f\u3011 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/78300.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010\u4ece0\u5f00\u59cb\u5b66\u8bbe\u8ba1\u6a21\u5f0f-4| \u5355\u4f8b\u6a21\u5f0f\u3011 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"Tip&#xff1a;\u672c\u7bc7\u7684\u4ee3\u7801\u5b9e\u73b0\u90fd\u662f\u57fa\u4e8eJava\u7684\u5b9e\u73b0\u3002 \u800c\u4e14\u7531\u4e8e\u4e0d\u540c\u8bed\u8a00\u7684\u7279\u6027\u4e0d\u540c\u3002\u4f8b\u5982c\u8ddfjava\u7684\u5185\u5b58\u7ba1\u7406\u662f\u4e0d\u4e00\u6837\u7684&#xff0c;\u8bed\u6cd5\u4e0a\u4e5f\u7565\u6709\u5dee\u522b&#xff0c;\u6240\u4ee5\u5355\u4f8b\u6a21\u5f0f\u7684\u5b9e\u73b0\u4e5f\u6709\u5dee\u522b\u3002\u4f8b\u5982C\u7684\u997f\u6c49\u6a21\u5f0f\u53ef\u4ee5\u6709&quot;\u975e\u5c40\u90e8\u9759\u6001\u5b9e\u73b0&quot;\u3001&quot;\u5c40\u90e8\u9759\u6001\u5b9e\u73b0&quot;\u7b49\u7b49&#xff0c;\u5982\u679c\u60f3\u4e86\u89e3C\u7248\u672c\u7684\u5355\u4f8b\u6a21\u5f0f\u81ea\u884c\u767e\u5ea6\u5373\u53ef\u6982\u8ff0 \u5bf9\u4e8e\u4e00\u4e2a\u8f6f\u4ef6\u7cfb\u7edf\u7684\u67d0\u4e9b\u7c7b\u800c\u8a00&#xff0c;\u6211\u4eec\u65e0\u987b\u521b\u5efa\u591a\u4e2a\u5b9e\u4f8b\u3002\u4e3a\u4e86\u8282\u7ea6\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u6709\u65f6\u9700\u8981\u786e\u4fdd\u7cfb\u7edf\u4e2d\u67d0\u4e2a\u7c7b\u53ea\u6709\u552f\u4e00\u4e00\u4e2a\u5b9e\u4f8b&#xff0c;\u5f53\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/78300.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-26T15:43:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226154306-69a06a0ab6ff0.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78300.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/78300.html\",\"name\":\"\u3010\u4ece0\u5f00\u59cb\u5b66\u8bbe\u8ba1\u6a21\u5f0f-4| \u5355\u4f8b\u6a21\u5f0f\u3011 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-26T15:43:12+00:00\",\"dateModified\":\"2026-02-26T15:43:12+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/78300.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/78300.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78300.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010\u4ece0\u5f00\u59cb\u5b66\u8bbe\u8ba1\u6a21\u5f0f-4| \u5355\u4f8b\u6a21\u5f0f\u3011\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010\u4ece0\u5f00\u59cb\u5b66\u8bbe\u8ba1\u6a21\u5f0f-4| \u5355\u4f8b\u6a21\u5f0f\u3011 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/78300.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010\u4ece0\u5f00\u59cb\u5b66\u8bbe\u8ba1\u6a21\u5f0f-4| \u5355\u4f8b\u6a21\u5f0f\u3011 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"Tip&#xff1a;\u672c\u7bc7\u7684\u4ee3\u7801\u5b9e\u73b0\u90fd\u662f\u57fa\u4e8eJava\u7684\u5b9e\u73b0\u3002 \u800c\u4e14\u7531\u4e8e\u4e0d\u540c\u8bed\u8a00\u7684\u7279\u6027\u4e0d\u540c\u3002\u4f8b\u5982c\u8ddfjava\u7684\u5185\u5b58\u7ba1\u7406\u662f\u4e0d\u4e00\u6837\u7684&#xff0c;\u8bed\u6cd5\u4e0a\u4e5f\u7565\u6709\u5dee\u522b&#xff0c;\u6240\u4ee5\u5355\u4f8b\u6a21\u5f0f\u7684\u5b9e\u73b0\u4e5f\u6709\u5dee\u522b\u3002\u4f8b\u5982C\u7684\u997f\u6c49\u6a21\u5f0f\u53ef\u4ee5\u6709\"\u975e\u5c40\u90e8\u9759\u6001\u5b9e\u73b0\"\u3001\"\u5c40\u90e8\u9759\u6001\u5b9e\u73b0\"\u7b49\u7b49&#xff0c;\u5982\u679c\u60f3\u4e86\u89e3C\u7248\u672c\u7684\u5355\u4f8b\u6a21\u5f0f\u81ea\u884c\u767e\u5ea6\u5373\u53ef\u6982\u8ff0 \u5bf9\u4e8e\u4e00\u4e2a\u8f6f\u4ef6\u7cfb\u7edf\u7684\u67d0\u4e9b\u7c7b\u800c\u8a00&#xff0c;\u6211\u4eec\u65e0\u987b\u521b\u5efa\u591a\u4e2a\u5b9e\u4f8b\u3002\u4e3a\u4e86\u8282\u7ea6\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u6709\u65f6\u9700\u8981\u786e\u4fdd\u7cfb\u7edf\u4e2d\u67d0\u4e2a\u7c7b\u53ea\u6709\u552f\u4e00\u4e00\u4e2a\u5b9e\u4f8b&#xff0c;\u5f53","og_url":"https:\/\/www.wsisp.com\/helps\/78300.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-26T15:43:12+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260226154306-69a06a0ab6ff0.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/78300.html","url":"https:\/\/www.wsisp.com\/helps\/78300.html","name":"\u3010\u4ece0\u5f00\u59cb\u5b66\u8bbe\u8ba1\u6a21\u5f0f-4| \u5355\u4f8b\u6a21\u5f0f\u3011 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-26T15:43:12+00:00","dateModified":"2026-02-26T15:43:12+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/78300.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/78300.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/78300.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010\u4ece0\u5f00\u59cb\u5b66\u8bbe\u8ba1\u6a21\u5f0f-4| \u5355\u4f8b\u6a21\u5f0f\u3011"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/78300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=78300"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/78300\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/78295"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=78300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=78300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=78300"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=78300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}