{"id":52549,"date":"2025-08-11T15:54:23","date_gmt":"2025-08-11T07:54:23","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/52549.html"},"modified":"2025-08-11T15:54:23","modified_gmt":"2025-08-11T07:54:23","slug":"java%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%ef%bc%88%e4%b8%80%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/52549.html","title":{"rendered":"Java\u8bbe\u8ba1\u6a21\u5f0f\uff08\u4e00\uff09"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F-toc\" style=\"margin-left:0px\">\u5355\u4f8b\u6a21\u5f0f<\/p>\n<p id=\"%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F-toc\" style=\"margin-left:0px\">\u5de5\u5382\u6a21\u5f0f<\/p>\n<p id=\"%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82-toc\" style=\"margin-left:40px\">\u62bd\u8c61\u5de5\u5382<\/p>\n<p id=\"1.%E5%AE%9A%E4%B9%89%E4%BA%A7%E5%93%81%E6%8E%A5%E5%8F%A3-toc\" style=\"margin-left:80px\">1.\u5b9a\u4e49\u4ea7\u54c1\u63a5\u53e3<\/p>\n<p id=\"2.%E5%85%B7%E4%BD%93%E4%BA%A7%E5%93%81%E5%AE%9E%E7%8E%B0-toc\" style=\"margin-left:80px\">2.\u5177\u4f53\u4ea7\u54c1\u5b9e\u73b0<\/p>\n<p id=\"3.%E5%AE%9A%E4%B9%89%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82%E6%8E%A5%E5%8F%A3-toc\" style=\"margin-left:80px\">3.\u5b9a\u4e49\u62bd\u8c61\u5de5\u5382\u63a5\u53e3<\/p>\n<p id=\"4.%E5%85%B7%E4%BD%93%E5%B7%A5%E5%8E%82%E5%AE%9E%E7%8E%B0-toc\" style=\"margin-left:80px\">4.\u5177\u4f53\u5de5\u5382\u5b9e\u73b0<\/p>\n<p id=\"5.%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BB%A3%E7%A0%81-toc\" style=\"margin-left:80px\">5.\u5ba2\u6237\u7aef\u4ee3\u7801<\/p>\n<p id=\"%E9%9D%99%E6%80%81%E5%B7%A5%E5%8E%82-toc\" style=\"margin-left:40px\">\u9759\u6001\u5de5\u5382<\/p>\n<p id=\"%E6%A8%A1%E7%89%88%E6%A8%A1%E5%BC%8F-toc\" style=\"margin-left:0px\">\u6a21\u7248\u6a21\u5f0f<\/p>\n<p id=\"1.%E6%8A%BD%E8%B1%A1%E7%B1%BB%E5%AE%9A%E4%B9%89%E7%AE%97%E6%B3%95%E9%AA%A8%E6%9E%B6-toc\" style=\"margin-left:80px\">1.\u62bd\u8c61\u7c7b\u5b9a\u4e49\u7b97\u6cd5\u9aa8\u67b6<\/p>\n<p id=\"2.%E5%85%B7%E4%BD%93%E5%AD%90%E7%B1%BB%E5%AE%9E%E7%8E%B0%E5%8F%AF%E5%8F%98%E9%83%A8%E5%88%86-toc\" style=\"margin-left:80px\">2.\u5177\u4f53\u5b50\u7c7b\u5b9e\u73b0\u53ef\u53d8\u90e8\u5206<\/p>\n<p id=\"3.%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BD%BF%E7%94%A8%E6%96%B9%E5%BC%8F-toc\" style=\"margin-left:80px\">3.\u5ba2\u6237\u7aef\u4f7f\u7528\u65b9\u5f0f<\/p>\n<p id=\"%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F-toc\" style=\"margin-left:0px\">\u7b56\u7565\u6a21\u5f0f<\/p>\n<p id=\"%E5%9F%BA%E4%BA%8E%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%9A%84%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F-toc\" style=\"margin-left:40px\">\u57fa\u4e8e\u9762\u5411\u5bf9\u8c61\u7684\u7b56\u7565\u6a21\u5f0f<\/p>\n<p id=\"%E5%9F%BA%E4%BA%8E%E9%9D%A2%E5%90%91%E5%87%BD%E6%95%B0%E6%8E%A5%E5%8F%A3%E7%9A%84%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F-toc\" style=\"margin-left:40px\">\u57fa\u4e8e\u9762\u5411\u51fd\u6570\u63a5\u53e3\u7684\u7b56\u7565\u6a21\u5f0f<\/p>\n<p id=\"%E4%BB%A3%E7%90%86%E6%A8%A1%E5%BC%8F-toc\" style=\"margin-left:0px\">\u4ee3\u7406\u6a21\u5f0f<\/p>\n<p id=\"%C2%A0%20%E9%9D%99%E6%80%81%E4%BB%A3%E7%90%86-toc\" style=\"margin-left:40px\">\u00a0 \u9759\u6001\u4ee3\u7406<\/p>\n<hr id=\"hr-toc\" \/>\n<h2 id=\"%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F\"><span style=\"color:#fe2c24\">\u5355\u4f8b\u6a21\u5f0f<\/span><\/h2>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color:#4da8ee\">\u00a0\u63cf\u8ff0&#xff1a;\u4e00\u4e2a\u7c7b\u6c38\u8fdc\u53ea\u4ea7\u751f\u8fd9\u4e2a\u7c7b\u7684\u552f\u4e00\u5bf9\u8c61\u3002<\/span><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6784\u9020\u51fd\u6570\u79c1\u6709&#xff0c;\u9650\u5236\u5728\u5916\u90e8\u7c7b\u4e2d\u53bb\u521b\u5efa\u5bf9\u8c61&#xff0c;\u82e5\u5728\u5916\u90e8\u80fd\u591f\u521b\u5efa\u5bf9\u8c61&#xff0c;\u4f1a\u5bfc\u81f4\u5404\u4e2a\u5bf9\u8c61\u5728\u5806\u5185\u5b58\u7684\u5b58\u50a8\u7684\u6570\u636e\u4e0d\u4e00\u81f4\u3002<\/p>\n<ul>\n<li>\u6076\u6c49\u5f0f\u5355\u4f8b &#xff1a;\u5728\u7c7b\u52a0\u8f7d\u65f6\u5c31\u521b\u5efa\u5bf9\u8c61&#xff0c;\u5360\u7528\u5806\u5185\u5b58\u3002<\/li>\n<li>\u61d2\u6c49\u5f0f\u5355\u4f8b&#xff1a;\u9700\u8981\u8fd9\u4e2a\u5bf9\u8c61\u7684\u65f6\u5019&#xff0c;\u624d\u53bb\u521b\u5efa&#xff0c;\u4e0d\u63d0\u524d\u5360\u7528\u5806\u5185\u5b58\u3002<\/li>\n<\/ul>\n<p>\/\/\u4e00\u4e2a\u7c7b\u6c38\u8fdc\u53ea\u4ea7\u751f\u8fd9\u4e2a\u7c7b\u7684\u552f\u4e00\u5bf9\u8c61<br \/>\npublic class ParseProperties {<\/p>\n<p>    \/\/\u6076\u6c49\u5f0f\u5355\u4f8b&#xff1a;\u6ca1\u6709\u8c03\u7528getInstance(),\u8fd9\u4e2a\u5bf9\u8c61\u5c31\u5b58\u5728\u3002\u5360\u7528\u5806\u5185\u5b58\u3002<br \/>\n\/\/    private static  ParseProperties p   &#061; new ParseProperties();<\/p>\n<p>    \/\/\u61d2\u6c49\u5f0f\u5355\u4f8b&#xff1a;\u9700\u8981\u8fd9\u4e2a\u5bf9\u8c61\u7684\u65f6\u5019&#xff0c;\u624d\u53bb\u521b\u5efa&#xff0c;\u61d2\u7684\u4f53\u73b0\u662f\u4e0d\u63d0\u524d\u5360\u7528\u5806\u5185\u5b58<br \/>\n    private static  ParseProperties p ;<\/p>\n<p>    \/\/\u6784\u9020\u51fd\u6570\u79c1\u6709&#xff0c;\u9650\u5236\u4e86\u5916\u90e8\u7c7b\u53bbnew\u5bf9\u8c61&#xff0c;\u521b\u5efa\u5bf9\u8c61<br \/>\n    private ParseProperties()<br \/>\n    {<\/p>\n<p>    }<\/p>\n<p>    \/\/\u901a\u8fc7\u7c7b\u540d.static\u65b9\u6cd5\u8c03\u7528<br \/>\n    public static ParseProperties  getInstance()<br \/>\n    {<br \/>\n        \/\/\u61d2\u6c49\u5f0f<br \/>\n        if(null &#061;&#061; p)<br \/>\n        {<br \/>\n            p &#061; new ParseProperties();<br \/>\n        }<\/p>\n<p>        return p;<br \/>\n    }<\/p>\n<p>} <\/p>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u4f53\u73b0\u4e86<span style=\"color:#fe2c24\">\u61d2\u6c49\u5f0f\u5355\u4f8b\u6a21\u5f0f<\/span>&#xff1a;<\/p>\n<p>&#xff08;1&#xff09;\u6784\u9020\u51fd\u6570\u79c1\u6709&#xff1a;\u9632\u6b62\u5916\u90e8\u7c7b\u76f4\u63a5\u901a\u8fc7new\u521b\u5efa\u5b9e\u4f8b&#xff0c;\u786e\u4fdd\u53ea\u80fd\u901a\u8fc7\u7c7b\u63d0\u4f9b\u7684\u9759\u6001\u65b9\u6cd5\u83b7\u53d6\u5b9e\u4f8b\u3002<\/p>\n<p>&#xff08;2&#xff09;\u9759\u6001\u79c1\u6709\u5b9e\u4f8b\u53d8\u91cf&#xff1a;\u8fd9\u4e2a\u9759\u6001\u53d8\u91cf\u7528\u4e8e\u4fdd\u5b58\u7c7b\u7684\u552f\u4e00\u5b9e\u4f8b\u3002<\/p>\n<p>&#xff08;3&#xff09;\u9759\u6001\u516c\u5171\u83b7\u53d6\u65b9\u6cd5&#xff1a;\u8fd9\u662f\u83b7\u53d6\u7c7b\u5b9e\u4f8b\u7684\u552f\u4e00\u5165\u53e3&#xff0c;\u63a7\u5236\u7740\u5b9e\u4f8b\u7684\u521b\u5efa\u548c\u8bbf\u95ee\u3002<\/p>\n<p>package demo1;<br \/>\nimport java.io.FileNotFoundException;<br \/>\nimport java.io.FileReader;<br \/>\nimport java.io.IOException;<br \/>\nimport java.io.Reader;<br \/>\nimport java.util.Properties;<\/p>\n<p>public class ParseProFile {<\/p>\n<p>    private static ParseProFile  p;  \/\/\u61d2\u6c49\u5f0f\u5355\u4f8b<\/p>\n<p>    \/\/ \u6784\u9020\u51fd\u6570\u79c1\u6709&#xff0c;\u9650\u5236\u5728\u5916\u90e8\u7c7b\u4e2d\u53bb\u521b\u5efa\u5bf9\u8c61&#xff0c;\u82e5\u5728\u5916\u90e8\u7c7b\u4e2d\u80fd\u591f\u521b\u5efa\u5bf9\u8c61&#xff0c;\u4f1a\u5bfc\u81f4\u5404\u4e2a\u5bf9\u8c61\u5728\u5806\u5185\u5b58\u7684\u5b58\u50a8\u7684\u6570\u636e\u4e0d\u4e00\u81f4<br \/>\n    private ParseProFile()<br \/>\n    {<br \/>\n        Properties p &#061; new Properties();<br \/>\n        try {<\/p>\n<p>            Reader reader &#061; new FileReader(&#034;.\/src\/main\/java\/demo1\/demo.properties&#034;);<br \/>\n            p.load(reader);<\/p>\n<p>            String str &#061; p.getProperty(&#034;port&#034;);<br \/>\n            System.out.println(str);<\/p>\n<p>            String queryStuSql &#061; p.getProperty(&#034;queryStuSQL&#034;);<br \/>\n            System.out.println(queryStuSql);<\/p>\n<p>        } catch (FileNotFoundException e) {<br \/>\n            e.printStackTrace();<br \/>\n        } catch (IOException e) {<br \/>\n            e.printStackTrace();<br \/>\n        }<\/p>\n<p>    }<\/p>\n<p>    public  static   ParseProFile   getInstance()<br \/>\n    {<\/p>\n<p>        if(null &#061;&#061; p)<br \/>\n        {<br \/>\n            p &#061;  new ParseProFile();  \/\/\u53ea\u6709\u5728\u7b2c\u4e00\u6b21\u8c03\u7528getInstance()\u65f6\u624d\u521b\u5efa\u5b9e\u4f8b&#xff0c;\u800c\u4e0d\u662f\u5728\u7c7b\u52a0\u8f7d\u65f6\u5c31\u521b\u5efa\u3002<br \/>\n        }<\/p>\n<p>        return p;    \/\/p\u662f\u9759\u6001\u53d8\u91cf&#xff0c;\u5b58\u5728\u4e8e\u9759\u6001\u5168\u5c40\u6570\u636e\u533a&#xff0c;\u5f53new\u8fc7\u4e00\u6b21\u540ep\u5b58\u5728,\u540e\u9762\u76f4\u63a5\u590d\u7528\u5c31\u884c&#xff0c;\u6240\u4ee5\u5730\u5740\u90fd\u76f8\u540c\u3002<\/p>\n<p>    }<br \/>\n}<br \/>\n\u5c5e\u6027\u6587\u4ef6&#xff1a;&#034;.\/src\/main\/java\/demo1\/demo.properties&#034;<br \/>\nport&#061;8090<br \/>\nqueryStuSQL&#061;select * from t_stus<br \/>\npackage demo1;<\/p>\n<p>public class Test {<\/p>\n<p>    public static void main(String[] args) {<\/p>\n<p>        ParseProFile  p1 &#061;   ParseProFile.getInstance();<br \/>\n        ParseProFile  p2 &#061;   ParseProFile.getInstance();<br \/>\n        ParseProFile  p3 &#061;   ParseProFile.getInstance();<\/p>\n<p>        \/\/\u591a\u6b21\u8c03\u7528getInstance()\u8fd4\u56de\u7684\u90fd\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002<br \/>\n        System.out.println(p1&#061;&#061;p2);<br \/>\n        System.out.println(p2&#061;&#061;p3);<br \/>\n    }<br \/>\n    \/\/ \u540e\u7eed\u8fd8\u9700\u8003\u8651\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<br \/>\n} <\/p>\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"179\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811075419-6899a1ab9f3d7.png\" width=\"451\" \/><\/p>\n<\/p>\n<p>\u4e0b\u9762\u662f\u901a\u8fc7\u9e3f\u8499&#xff08;HarmonyOS&#xff09;ArkUI \u7684\u5e03\u5c40\u793a\u4f8b&#xff0c;\u4f53\u73b0\u7c7b\u4f3c\u6076\u6c49\u5f0f\u548c\u61d2\u6c49\u5f0f\u7684\u5355\u4f8b\u6a21\u5f0f&#xff0c;\u5b83\u4eec\u53ef\u4ee5\u901a\u8fc7<span style=\"color:#fe2c24\">\u00a0\u6570\u636e\u52a0\u8f7d\u548c\u6e32\u67d3\u65f6\u673a\u00a0<\/span>\u6765\u7c7b\u6bd4\u8fd9\u4e24\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u7684\u601d\u60f3\u3002<\/p>\n<p>&#xff08;1&#xff09;\u4f53\u73b0\u6076\u6c49\u5f0f\u5355\u4f8b\u6a21\u5f0f&#xff1a;&#xff08;Scroll &#043; ForEach\u5e03\u5c40&#xff09;<\/p>\n<ul>\n<li>\n<p>\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e&#xff08;100 \u9879\u5168\u90e8\u521d\u59cb\u5316&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u5373\u4f7f\u67d0\u4e9b\u00a0Row\u00a0\u4e0d\u5728\u53ef\u89c6\u533a\u57df&#xff0c;\u4e5f\u4f1a\u88ab\u00a0ForEach\u00a0\u904d\u5386\u5e76\u521b\u5efa\u3002<\/p>\n<\/li>\n<li>\n<p>\u7c7b\u4f3c\u4e8e\u6076\u6c49\u5f0f&#xff1a;\u63d0\u524d\u52a0\u8f7d\u6240\u6709\u6570\u636e&#xff0c;\u5360\u7528\u5185\u5b58\u8f83\u591a&#xff0c;\u4f46\u6eda\u52a8\u65f6\u4e0d\u4f1a\u52a8\u6001\u52a0\u8f7d\u3002<\/p>\n<\/li>\n<\/ul>\n<p>&#064;Entry<br \/>\n&#064;Component<br \/>\nstruct ScrollPage {<\/p>\n<p>  arrs:string[] &#061; []<\/p>\n<p>  aboutToAppear(): void {<br \/>\n    for(let i &#061;0;i&lt;100;i&#043;&#043;)<br \/>\n    {<br \/>\n      this.arrs.push(&#034;\u7b2c&#034; &#043; i &#043; &#034;\u9879&#034;);<br \/>\n    }<br \/>\n  }<\/p>\n<p>  loadItem(item:string,index:number):string{<br \/>\n    console.log(&#034;\u52a0\u8f7d\u7684\u9879\u662f&#xff1a;&#034; &#043;index)<br \/>\n    return item<br \/>\n  }<\/p>\n<p>  build() {<\/p>\n<p>    Scroll()<br \/>\n    {<br \/>\n      Column()<br \/>\n      {<br \/>\n        ForEach(this.arrs,(item:string,index:number)&#061;&gt;{<br \/>\n          Row()<br \/>\n          {<br \/>\n            Text(this.loadItem(item,index)).fontSize(30)<br \/>\n          }.width(&#034;100%&#034;).height(&#034;10%&#034;).backgroundColor(&#034;#aabbcc&#034;)<br \/>\n          .margin({top:10}).borderRadius(20).justifyContent(FlexAlign.Center)<br \/>\n        })<br \/>\n      }<\/p>\n<p>    }.width(&#034;100%&#034;).height(&#034;100%&#034;)<\/p>\n<p>  }<br \/>\n} <\/p>\n<p>\u8fd0\u884c\u6548\u679c\u5982\u4e0b&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"327\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811075419-6899a1abbc0eb.png\" width=\"699\" \/><\/p>\n<p>&#xff08;2&#xff09;\u4f53\u73b0\u61d2\u6c49\u5f0f\u5355\u4f8b\u6a21\u5f0f&#xff1a;&#xff08;List &#043; ForEach\u5e03\u5c40&#xff09;<\/p>\n<ul>\n<li>\n<p>\u6309\u9700\u6e32\u67d3&#xff1a;List\u00a0\u7ec4\u4ef6\u81ea\u5e26\u61d2\u52a0\u8f7d\u4f18\u5316\u3002<\/p>\n<\/li>\n<li>\n<p>\u53ea\u6709\u6eda\u52a8\u5230\u67d0\u4e2a\u00a0ListItem\u00a0\u65f6&#xff0c;\u624d\u4f1a\u771f\u6b63\u521b\u5efa\u5e76\u6e32\u67d3\u5b83\u3002<\/p>\n<\/li>\n<li>\n<p>\u7c7b\u4f3c\u4e8e\u61d2\u6c49\u5f0f&#xff1a;\u4ec5\u52a0\u8f7d\u5f53\u524d\u53ef\u89c1\u7684\u9879&#xff0c;\u8282\u7701\u5185\u5b58&#xff0c;\u4f46\u6eda\u52a8\u65f6\u53ef\u80fd\u9700\u8981\u52a8\u6001\u52a0\u8f7d\u3002<\/p>\n<\/li>\n<\/ul>\n<p>&#064;Entry<br \/>\n&#064;Component<br \/>\nstruct ListPage {<br \/>\n  arrs: string[] &#061; []<\/p>\n<p>  aboutToAppear(): void {<br \/>\n    for (let i &#061; 0; i &lt; 100; i&#043;&#043;) {<br \/>\n      this.arrs.push(&#034;\u7b2c&#034; &#043; i &#043; &#034;\u9879&#034;);<br \/>\n    }<br \/>\n  }<\/p>\n<p>  loadItem(item: string, index: number): string {<br \/>\n    console.log(&#034;\u52a0\u8f7d\u7684\u9879\u662f&#xff1a;&#034; &#043; index)<br \/>\n    return item<br \/>\n  }<\/p>\n<p>  build() {<br \/>\n    List() {<br \/>\n      ForEach(this.arrs, (item: string, index: number) &#061;&gt; {<br \/>\n        ListItem() {<br \/>\n          Text(this.loadItem(item, index)).fontSize(30)<br \/>\n        }<br \/>\n        .width(&#034;100%&#034;)<br \/>\n        .height(&#034;10%&#034;)<br \/>\n        .backgroundColor(&#034;#aabbcc&#034;)<br \/>\n        .margin({ top: 10 })<br \/>\n        .borderRadius(20)<br \/>\n      })<br \/>\n    }.width(&#034;100%&#034;).height(&#034;100%&#034;)<br \/>\n  }<\/p>\n<p>} <\/p>\n<p>\u8fd0\u884c\u6548\u679c\u5982\u4e0b&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"896\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811075420-6899a1aca1df6.png\" width=\"1917\" \/><\/p>\n<p>\u603b\u7ed3&#xff1a;<\/p>\n<table align=\"center\" border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:800px\">\n<tr>\u7279\u6027\u6076\u6c49\u5f0f\u61d2\u6c49\u5f0f<\/tr>\n<tbody>\n<tr>\n<td style=\"text-align:center;vertical-align:middle\">\u521d\u59cb\u5316\u65f6\u673a<\/td>\n<td style=\"width:315px\">\u63d0\u524d\u52a0\u8f7d&#xff08;\u5982\u00a0aboutToAppear\u00a0\u65f6\u5c31\u521d\u59cb\u5316&#xff09;<\/td>\n<td style=\"width:373px\">\u6309\u9700\u52a0\u8f7d&#xff08;\u5982\u6eda\u52a8\u5230\u67d0\u4e2a\u4f4d\u7f6e\u624d\u52a0\u8f7d&#xff09;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;vertical-align:middle\">\u5185\u5b58\u5360\u7528<\/td>\n<td style=\"width:315px\">\u53ef\u80fd\u5360\u7528\u8f83\u591a\u5185\u5b58&#xff08;\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e&#xff09;<\/td>\n<td style=\"width:373px\">\u8282\u7701\u5185\u5b58&#xff08;\u4ec5\u52a0\u8f7d\u90e8\u5206&#xff09;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;vertical-align:middle\">\u6027\u80fd\u5f71\u54cd<\/td>\n<td style=\"width:315px\">\u9996\u6b21\u52a0\u8f7d\u53ef\u80fd\u8f83\u6162<\/td>\n<td style=\"width:373px\">\u6eda\u52a8\u65f6\u53ef\u80fd\u5361\u987f&#xff08;\u52a8\u6001\u52a0\u8f7d&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F\"><span style=\"color:#fe2c24\">\u5de5\u5382\u6a21\u5f0f<\/span><\/h2>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color:#4da8ee\">\u63cf\u8ff0&#xff1a;\u6839\u636e\u7528\u6237\u7684\u9700\u6c42\u5de5\u5382\u7c7b\u4ea7\u751f\u5b9e\u9645\u7684\u5bf9\u8c61\u3002\u4e3b\u8981\u662f\u7c7b\u7684\u8bbe\u8ba1&#xff0c;\u52a8\u6001\u7ed1\u5b9a&#xff0c;\u65b9\u6cd5\u91cd\u5199\u3002<\/span><\/p>\n<h3 id=\"%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82\">\u62bd\u8c61\u5de5\u5382<\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e00\u4e2a\u5de5\u5382\u7c7b\u521b\u5efa\u4e00\u4e2a\u4ea7\u54c1\u65cf&#xff08;\u591a\u4e2a\u76f8\u5173\u4ea7\u54c1&#xff09;\u3002<\/p>\n<ul>\n<li>\u62bd\u8c61\u4e0e\u5b9e\u73b0\u5206\u79bb&#xff1a;\u5ba2\u6237\u7aef\u4ee3\u7801\u53ea\u4f9d\u8d56\u62bd\u8c61\u63a5\u53e3&#xff0c;\u4e0d\u4f9d\u8d56\u5177\u4f53\u5b9e\u73b0<\/li>\n<li>\u6613\u4e8e\u6269\u5c55&#xff1a;\u8981\u6dfb\u52a0\u65b0\u7684\u4ea7\u54c1\u7c7b\u578b&#xff0c;\u53ea\u9700\u8981\u6dfb\u52a0\u65b0\u7684\u5de5\u5382\u7c7b\u548c\u4ea7\u54c1\u7c7b&#xff0c;\u4e0d\u9700\u8981\u4fee\u6539\u73b0\u6709\u4ee3\u7801<\/li>\n<li>\u52a8\u6001\u7ed1\u5b9a&#xff1a;\u901a\u8fc7\u8fd0\u884c\u65f6\u51b3\u5b9a\u4f7f\u7528\u54ea\u4e2a\u5177\u4f53\u5de5\u5382<\/li>\n<li>\u5355\u4e00\u804c\u8d23&#xff1a;\u6bcf\u4e2a\u5de5\u5382\u53ea\u8d1f\u8d23\u521b\u5efa\u81ea\u5df1\u4ea7\u54c1\u65cf\u7684\u4ea7\u54c1<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u52a8\u7269\u519c\u573a\u6a21\u62df\u5668\u6765\u4f53\u73b0\u62bd\u8c61\u5de5\u5382\u6a21\u5f0f\u7684\u6838\u5fc3\u601d\u60f3\u548c\u7ed3\u6784&#xff1a;<\/p>\n<h4 id=\"1.%E5%AE%9A%E4%B9%89%E4%BA%A7%E5%93%81%E6%8E%A5%E5%8F%A3\">1.\u5b9a\u4e49\u4ea7\u54c1\u63a5\u53e3<\/h4>\n<p>&#xff08;1&#xff09;\u52a8\u7269\u63a5\u53e3<\/p>\n<p>\/\/\u52a8\u7269\u63a5\u53e3<br \/>\npublic interface Animal {<br \/>\n    void makeSound();<br \/>\n    void eat();<br \/>\n} <\/p>\n<p>&#xff08;2&#xff09;\u98df\u7269\u63a5\u53e3<\/p>\n<p>\/\/ \u98df\u7269\u63a5\u53e3<br \/>\npublic interface Food {<br \/>\n    void showNutrition();<br \/>\n} <\/p>\n<h4 id=\"2.%E5%85%B7%E4%BD%93%E4%BA%A7%E5%93%81%E5%AE%9E%E7%8E%B0\">2.\u5177\u4f53\u4ea7\u54c1\u5b9e\u73b0<\/h4>\n<p>&#xff08;1&#xff09;\u732b\u4ea7\u54c1\u65cf<\/p>\n<p>public class Cat implements Animal {<br \/>\n    &#064;Override<br \/>\n    public void makeSound() {<br \/>\n        System.out.println(&#034;\u55b5\u55b5\u55b5~&#034;);<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public void eat() {<br \/>\n        System.out.println(&#034;\u732b\u5728\u5403\u9c7c&#034;);<br \/>\n    }<br \/>\n}<br \/>\npublic class Fish implements Food {<br \/>\n    &#064;Override<br \/>\n    public void showNutrition() {<br \/>\n        System.out.println(&#034;\u9c7c\u7c7b\u98df\u7269&#xff1a;\u5bcc\u542b\u86cb\u767d\u8d28\u548cOmega-3&#034;);<br \/>\n    }<br \/>\n} <\/p>\n<p>&#xff08;2&#xff09;\u72d7\u4ea7\u54c1\u65cf<\/p>\n<p>public class Dog implements Animal {<br \/>\n    &#064;Override<br \/>\n    public void makeSound() {<br \/>\n        System.out.println(&#034;\u6c6a\u6c6a\u6c6a!&#034;);<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public void eat() {<br \/>\n        System.out.println(&#034;\u72d7\u5728\u5543\u9aa8\u5934&#034;);<br \/>\n    }<br \/>\n}<br \/>\npublic class Bone implements Food {<br \/>\n    &#064;Override<br \/>\n    public void showNutrition() {<br \/>\n        System.out.println(&#034;\u9aa8\u5934&#xff1a;\u5bcc\u542b\u9499\u8d28&#xff0c;\u6709\u52a9\u4e8e\u7259\u9f7f\u5065\u5eb7&#034;);<br \/>\n    }<br \/>\n} <\/p>\n<p>\u4f53\u73b0\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4ea7\u54c1\u5206\u4e3a\u62bd\u8c61\u63a5\u53e3\u548c\u5177\u4f53\u5b9e\u73b0<\/p>\n<\/li>\n<li>\n<p>\u4ea7\u54c1\u6309\u7c7b\u578b\u5f62\u6210\u7b49\u7ea7\u7ed3\u6784&#xff08;\u52a8\u7269\u3001\u98df\u7269&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u5177\u4f53\u4ea7\u54c1\u5c5e\u4e8e\u7279\u5b9a\u4ea7\u54c1\u65cf&#xff08;\u732b\u65cf\u3001\u72d7\u65cf&#xff09;<\/p>\n<\/li>\n<\/ul>\n<h4 id=\"3.%E5%AE%9A%E4%B9%89%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82%E6%8E%A5%E5%8F%A3\">3.\u5b9a\u4e49\u62bd\u8c61\u5de5\u5382\u63a5\u53e3<\/h4>\n<p>public interface AnimalFarmFactory {<br \/>\n    Animal createAnimal();<br \/>\n    Food createFood();<br \/>\n} <\/p>\n<p>\u4f53\u73b0\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5b9a\u4e49\u4e86\u521b\u5efa&#034;\u4ea7\u54c1\u65cf&#034;\u7684\u63a5\u53e3&#xff08;\u52a8\u7269\u548c\u98df\u7269&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u4e0d\u5173\u5fc3\u5177\u4f53\u5b9e\u73b0&#xff0c;\u53ea\u58f0\u660e\u80fd\u521b\u5efa\u54ea\u4e9b\u7c7b\u578b\u7684\u4ea7\u54c1<\/p>\n<\/li>\n<li>\n<p>\u7b26\u5408&#034;\u62bd\u8c61\u5de5\u5382&#034;\u4e2d&#034;\u62bd\u8c61&#034;\u7684\u6982\u5ff5<\/p>\n<\/li>\n<\/ul>\n<h4 id=\"4.%E5%85%B7%E4%BD%93%E5%B7%A5%E5%8E%82%E5%AE%9E%E7%8E%B0\">4.\u5177\u4f53\u5de5\u5382\u5b9e\u73b0<\/h4>\n<p>&#xff08;1&#xff09;\u732b\u5de5\u5382<\/p>\n<p>public class CatFarmFactory implements AnimalFarmFactory {<br \/>\n    &#064;Override<br \/>\n    public Animal createAnimal() {<br \/>\n        return new Cat();<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public Food createFood() {<br \/>\n        return new Fish();<br \/>\n    }<br \/>\n} <\/p>\n<p>&#xff08;2&#xff09;\u72d7\u5de5\u5382<\/p>\n<p>public class DogFarmFactory implements AnimalFarmFactory {<br \/>\n    &#064;Override<br \/>\n    public Animal createAnimal() {<br \/>\n        return new Dog();<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public Food createFood() {<br \/>\n        return new Bone();<br \/>\n    }<br \/>\n} <\/p>\n<p>\u4f53\u73b0\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6bcf\u4e2a\u5177\u4f53\u5de5\u5382\u8d1f\u8d23\u521b\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684\u4ea7\u54c1\u65cf&#xff08;\u732b&#043;\u9c7c&#xff0c;\u72d7&#043;\u9aa8\u5934)<\/p>\n<\/li>\n<li>\n<p>\u7b26\u5408&#034;\u4e00\u4e2a\u5de5\u5382\u521b\u5efa\u4e00\u7cfb\u5217\u76f8\u5173\u5bf9\u8c61&#034;\u7684\u539f\u5219<\/p>\n<\/li>\n<\/ul>\n<h4 id=\"5.%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BB%A3%E7%A0%81\">5.\u5ba2\u6237\u7aef\u4ee3\u7801<\/h4>\n<p>package abstratcDemo2;<\/p>\n<p>import java.util.Scanner;<\/p>\n<p>public class FarmSimulator {<br \/>\n    public static void main(String[] args) {<br \/>\n        System.out.println(&#034;\u6b22\u8fce\u6765\u5230\u52a8\u7269\u519c\u573a\u6a21\u62df\u5668&#xff01;&#034;);<br \/>\n        System.out.println(&#034;\u8bf7\u9009\u62e9\u8981\u521b\u5efa\u7684\u519c\u573a\u7c7b\u578b&#xff1a;1. \u732b\u519c\u573a 2. \u72d7\u519c\u573a&#034;);<\/p>\n<p>        Scanner scanner &#061; new Scanner(System.in);<br \/>\n        int choice &#061; scanner.nextInt();<\/p>\n<p>        AnimalFarmFactory factory &#061; null;<\/p>\n<p>        if(choice &#061;&#061; 1){<br \/>\n            factory &#061; new CatFarmFactory();     \/\/\u52a8\u6001\u7ed1\u5b9a<br \/>\n        }<br \/>\n        else if(choice &#061;&#061; 2){<br \/>\n            factory &#061; new DogFarmFactory();     \/\/\u52a8\u6001\u7ed1\u5b9a<br \/>\n        }<\/p>\n<p>        \/\/ \u4f7f\u7528\u5de5\u5382\u521b\u5efa\u4ea7\u54c1&#xff08;\u65b9\u6cd5\u91cd\u5199&#xff09;<br \/>\n        Animal animal &#061; factory.createAnimal();<br \/>\n        Food food &#061; factory.createFood();<\/p>\n<p>        System.out.println(&#034;\u519c\u573a\u521b\u5efa\u6210\u529f&#xff1a;&#034;);<br \/>\n        System.out.print(&#034;\u52a8\u7269\u884c\u4e3a&#xff1a;&#034;);<br \/>\n        animal.makeSound();<\/p>\n<p>        System.out.print(&#034;\u8fdb\u98df\u884c\u4e3a&#xff1a;&#034;);<br \/>\n        animal.eat();<\/p>\n<p>        System.out.print(&#034;\u98df\u7269\u8425\u517b&#xff1a;&#034;);<br \/>\n        food.showNutrition();<\/p>\n<p>    }<br \/>\n}<\/p>\n<p>\u4f53\u73b0\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5ba2\u6237\u7aef\u53ea\u4f9d\u8d56\u62bd\u8c61\u63a5\u53e3&#xff0c;\u4e0d\u4f9d\u8d56\u5177\u4f53\u5b9e\u73b0<\/p>\n<\/li>\n<li>\n<p>\u53ef\u4ee5\u8f7b\u677e\u5207\u6362\u6574\u4e2a\u4ea7\u54c1\u65cf&#xff08;\u53ea\u9700\u5207\u6362\u52a8\u6001\u7ed1\u5b9a\u7684\u5de5\u5382\u5b9e\u4f8b&#xff09;<\/p>\n<\/li>\n<\/ul>\n<p>\u5b9e\u73b0\u6548\u679c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"267\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811075421-6899a1adc5f62.png\" width=\"847\" \/><\/p>\n<\/p>\n<h3 id=\"%E9%9D%99%E6%80%81%E5%B7%A5%E5%8E%82\">\u9759\u6001\u5de5\u5382<\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5de5\u5382\u7c7b\u91cc\u9762\u5199\u9759\u6001\u65b9\u6cd5&#xff0c;\u901a\u8fc7\u7c7b\u540d\u8c03\u7528\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6240\u6709\u5bf9\u8c61\u7684\u521b\u5efa\u903b\u8f91\u96c6\u4e2d\u5728\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\u4e2d&#xff0c;\u4e0d\u9700\u8981\u521b\u5efa\u591a\u4e2a\u5de5\u5382\u5b50\u7c7b\u3002<\/p>\n<p>\u9759\u6001\u5de5\u5382\u7279\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u4e0d\u9700\u8981\u5b9e\u4f8b\u5316\u5de5\u5382\u7c7b<\/li>\n<li>\u901a\u8fc7\u7c7b\u540d\u76f4\u63a5\u8c03\u7528\u9759\u6001\u65b9\u6cd5<\/li>\n<li>\u901a\u5e38\u7528\u4e8e\u521b\u5efa\u903b\u8f91\u7b80\u5355\u3001\u4e0d\u9700\u8981\u7ef4\u62a4\u72b6\u6001\u7684\u573a\u666f<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u9759\u6001\u5de5\u5382\u6a21\u5f0f\u7684\u5b9e\u73b0\u65b9\u5f0f&#xff0c;\u5de5\u5382\u7c7bAnimalFactory\u5305\u542b\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5createAnimal&#xff0c;\u5ba2\u6237\u7aef\u901a\u8fc7\u7c7b\u540d\u76f4\u63a5\u8c03\u7528\u9759\u6001\u65b9\u6cd5\u521b\u5efa\u5bf9\u8c61&#xff1a;AnimalFactory.createAnimal()&#xff0c;\u5c06\u5bf9\u8c61\u521b\u5efa\u7684\u7ec6\u8282\u5c01\u88c5\u5728\u5de5\u5382\u7c7b\u4e2d\u3002<\/p>\n<p>public interface Animal {<br \/>\n    public  void  makeAnimal();<br \/>\n}<br \/>\npublic class AnimalFactory {<\/p>\n<p>    \/\/ \u5de5\u5382\u7c7b\u91cc\u9762\u5199\u9759\u6001\u65b9\u6cd5&#xff0c;\u901a\u8fc7\u7c7b\u540d\u8c03\u7528<br \/>\n    public  static  Animal  createAnimal(String name)<br \/>\n    {<br \/>\n        Animal  an  &#061; null;<\/p>\n<p>        if(name.equals(&#034;\u72d7&#034;))<br \/>\n        {<br \/>\n            an &#061; new Dog();<br \/>\n        }<br \/>\n        if(name.equals(&#034;\u732b&#034;))<br \/>\n        {<br \/>\n            an  &#061; new Cat();<br \/>\n        }<\/p>\n<p>        \/\/\u5f53\u4ea7\u54c1\u7c7b\u578b\u5f88\u591a\u65f6&#xff0c;\u9759\u6001\u5de5\u5382\u9700\u8981\u5927\u91cfif-else\u5206\u652f<br \/>\n        \/\/\u9762\u5411\u5bf9\u8c61\u89e3\u51b3\u4e0d\u4e86\u7684\u95ee\u9898&#xff0c;\u540e\u7eed\u901a\u8fc7\u9762\u5411\u53cd\u5c04\u624d\u53ef\u4ee5\u89e3\u51b3<br \/>\n        return an;<br \/>\n    }<br \/>\n}<\/p>\n<p>public class Cat implements Animal {<br \/>\n    &#064;Override<br \/>\n    public void makeAnimal() {<br \/>\n        System.out.println(&#034;\u751f\u4ea7\u732b&#034;);<br \/>\n    }<br \/>\n}<br \/>\npublic class Dog implements Animal {<br \/>\n    &#064;Override<br \/>\n    public void makeAnimal() {<\/p>\n<p>        System.out.println(&#034;\u751f\u4ea7\u72d7&#034;);<br \/>\n    }<br \/>\n}<br \/>\npackage staticDemo;<\/p>\n<p>import java.util.Scanner;<\/p>\n<p>public class Test {<\/p>\n<p>    public static void main(String[] args) {<br \/>\n        System.out.println(&#034;\u8bf7\u95ee\u60a8\u9700\u8981\u5236\u9020\u4ec0\u4e48\u52a8\u7269&#xff1f;&#034;);<\/p>\n<p>        Scanner s &#061; new Scanner(System.in);<br \/>\n        String inputValue &#061; s.next();<\/p>\n<p>        Animal an &#061; AnimalFactory.createAnimal(inputValue);<br \/>\n        an.makeAnimal();<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8fd0\u884c\u6548\u679c\u5982\u4e0b&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"141\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811075422-6899a1ae04434.png\" width=\"545\" \/><\/p>\n<\/p>\n<h2 id=\"%E6%A8%A1%E7%89%88%E6%A8%A1%E5%BC%8F\"><span style=\"color:#fe2c24\">\u6a21\u7248\u6a21\u5f0f<\/span><\/h2>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color:#4da8ee\">\u63cf\u8ff0&#xff1a;\u901a\u8fc7\u6a21\u677f\u65b9\u6cd5\u6309\u7167\u89c4\u5b9a\u597d\u7684\u6d41\u7a0b\u6267\u884c\u3002<\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u56fa\u5b9a\u6d41\u7a0b\u63a7\u5236&#xff1a;<\/span>\u62bd\u8c61\u7c7b\u4e2d\u7684\u6a21\u7248\u65b9\u6cd5\u5b9a\u4e49\u4e86\u4e0d\u53ef\u53d8\u7684\u7b97\u6cd5\u9aa8\u67b6&#xff0c;\u4f7f\u7528final\u4fee\u9970\u9632\u6b62\u5b50\u7c7b\u4fee\u6539\u6d41\u7a0b\u3002<\/li>\n<li><span style=\"color:#0d0016\">\u53ef\u53d8\u90e8\u5206\u62bd\u8c61\u5316<\/span>&#xff1a;\u5c06\u53ef\u80fd\u53d8\u5316\u7684\u90e8\u5206\u58f0\u660e\u4e3a\u62bd\u8c61\u65b9\u6cd5&#xff0c;\u7531\u5b50\u7c7b\u5b9e\u73b0\u5177\u4f53\u884c\u4e3a\u3002<\/li>\n<li><span style=\"color:#0d0016\">\u4ee3\u7801\u590d\u7528<\/span>&#xff1a;\u516c\u5171\u884c\u4e3a\u5728\u62bd\u8c61\u7c7b\u4e2d\u5b9e\u73b0&#xff0c;\u907f\u514d\u5b50\u7c7b\u91cd\u590d\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u4e0b\u9762\u901a\u8fc7\u4ee3\u7801\u4f53\u73b0\u6a21\u7248\u6a21\u5f0f\u7684\u7ed3\u6784\u548c\u7279\u70b9&#xff1a;<\/span><\/p>\n<h4 id=\"1.%E6%8A%BD%E8%B1%A1%E7%B1%BB%E5%AE%9A%E4%B9%89%E7%AE%97%E6%B3%95%E9%AA%A8%E6%9E%B6\">1.\u62bd\u8c61\u7c7b\u5b9a\u4e49\u7b97\u6cd5\u9aa8\u67b6<\/h4>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5728\u62bd\u8c61\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u6a21\u677f\u65b9\u6cd5&#xff08;\u6267\u884c\u6d41\u7a0b\u4e0d\u5e0c\u671b\u88ab\u6539\u53d8&#xff0c;\u7528final\u5173\u952e\u5b57\u4fee\u9970&#xff0c;\u8be5\u65b9\u6cd5\u4e0d\u88ab\u5b50\u7c7b\u7ee7\u627f&#xff09;\u3002<\/p>\n<p>public abstract class AbstractHome {<\/p>\n<p>    \/\/\u5b9a\u4e49\u4e00\u4e2a\u6a21\u677f\u65b9\u6cd5&#xff0c;\u7528final\u5173\u952e\u5b57\u4fee\u9970&#xff0c;\u8be5\u65b9\u6cd5\u4e0d\u88ab\u5b50\u7c7b\u7ee7\u627f<br \/>\n    public  final  void  homeFrameWork()<br \/>\n    {<br \/>\n        this.work();<br \/>\n        this.goHomeType();<br \/>\n        this.goHome();<br \/>\n    }<\/p>\n<p>    public  void  work()<br \/>\n    {<br \/>\n        System.out.println(&#034;\u5de5\u4f5c\u5230\u70b9\u4e0b\u73ed&#034;);<br \/>\n    }<\/p>\n<p>    \/\/\u56de\u5bb6\u7684\u4ea4\u901a\u5de5\u5177\u4e0d\u4e00\u6837&#xff0c;\u62bd\u8c61\u65b9\u6cd5&#xff0c;\u7531\u5b50\u7c7b\u5b9e\u73b0<br \/>\n    public  abstract  void   goHomeType();<\/p>\n<p>    public  void    goHome()<br \/>\n    {<br \/>\n        System.out.println(&#034;\u5230\u5bb6\u4e86&#034;);<br \/>\n    }<br \/>\n} <\/p>\n<h4 id=\"2.%E5%85%B7%E4%BD%93%E5%AD%90%E7%B1%BB%E5%AE%9E%E7%8E%B0%E5%8F%AF%E5%8F%98%E9%83%A8%E5%88%86\">2.\u5177\u4f53\u5b50\u7c7b\u5b9e\u73b0\u53ef\u53d8\u90e8\u5206<\/h4>\n<p>public class HuaweiCar  extends AbstractHome {<br \/>\n    &#064;Override<br \/>\n    public void goHomeType() {<br \/>\n        System.out.println(&#034;\u4e58\u5750\u534e\u4e3a\u6c7d\u8f66\u56de\u5bb6&#034;);<br \/>\n    }<br \/>\n}<br \/>\npublic class XiaomiCar  extends AbstractHome {<br \/>\n    &#064;Override<br \/>\n    public void goHomeType() {<br \/>\n        System.out.println(&#034;\u4e58\u5750\u5c0f\u7c73\u6c7d\u8f66\u56de\u5bb6&#034;);<br \/>\n    }<br \/>\n} <\/p>\n<h4 id=\"3.%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BD%BF%E7%94%A8%E6%96%B9%E5%BC%8F\">3.\u5ba2\u6237\u7aef\u4f7f\u7528\u65b9\u5f0f<\/h4>\n<p>public class Test {<\/p>\n<p>    public static void main(String[] args) {<br \/>\n        AbstractHome  ah  &#061; new HuaweiCar();<br \/>\n\/\/        AbstractHome  ah  &#061; new XiaomiCar();<\/p>\n<p>        ah.homeFrameWork();<br \/>\n    }<\/p>\n<p>} <\/p>\n<p>\u8fd0\u884c\u6548\u679c\u5982\u4e0b&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"142\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811075422-6899a1ae16124.png\" width=\"557\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"143\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811075422-6899a1ae2a654.png\" width=\"562\" \/><\/p>\n<\/p>\n<h2 id=\"%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F\"><span style=\"color:#fe2c24\">\u7b56\u7565\u6a21\u5f0f<\/span><\/h2>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color:#4da8ee\">\u00a0\u63cf\u8ff0&#xff1a;\u7b56\u7565\u7c7b\u6765\u7ed1\u5b9a&#xff08;\u901a\u8fc7\u6784\u9020\u51fd\u6570\u6216set\u65b9\u6cd5&#xff09;\u4e0d\u540c\u7684\u5177\u4f53\u7684\u7b56\u7565\u7684\u5b9e\u73b0\u7c7b\u3002<\/span><\/p>\n<h3 id=\"%E5%9F%BA%E4%BA%8E%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%9A%84%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F\">\u57fa\u4e8e\u9762\u5411\u5bf9\u8c61\u7684\u7b56\u7565\u6a21\u5f0f<\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color:#4da8ee\">\u7b56\u7565\u7c7b\u4e2d\u7684\u6784\u9020\u6ce8\u5165\u6216set\u6ce8\u5165\u4f20\u5165\u7684\u53c2\u6570\u662f\u5bf9\u8c61\u7c7b\u578b\u3002<\/span><\/p>\n<ul>\n<li>\u6d88\u9664\u6761\u4ef6\u8bed\u53e5&#xff1a;\u907f\u514d\u4e86\u5728\u5ba2\u6237\u7aef\u4f7f\u7528\u5927\u91cf\u7684if-else\u6216switch-case\u6765\u9009\u62e9\u7b97\u6cd5<\/li>\n<li>\u7b97\u6cd5\u590d\u7528&#xff1a;\u540c\u4e00\u4e2a\u7b56\u7565\u53ef\u4ee5\u88ab\u591a\u4e2a\u4e0a\u4e0b\u6587\u4f7f\u7528<\/li>\n<li>\u7075\u6d3b\u6269\u5c55&#xff1a;\u6dfb\u52a0\u65b0\u7b56\u7565\u4e0d\u5f71\u54cd\u73b0\u6709\u7cfb\u7edf<\/li>\n<li>\u8fd0\u884c\u65f6\u52a8\u6001\u5207\u6362&#xff1a;\u5982\u793a\u4f8b\u4e2d\u901a\u8fc7setLg()\u65b9\u6cd5\u5207\u6362\u7b56\u7565<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u4f53\u73b0\u4e86\u57fa\u4e8e\u9762\u5411\u5bf9\u8c61\u7684\u7b56\u7565\u6a21\u5f0f\u7684\u5b9e\u73b0&#xff1a;<\/p>\n<p>1.\u7b56\u7565\u63a5\u53e3 (LikeGril)<\/p>\n<p>public interface LikeGril {<br \/>\n    public void likeCategory(String type);<br \/>\n} <\/p>\n<ul>\n<li>\n<p>\u5b9a\u4e49\u4e86\u6240\u6709\u5177\u4f53\u7b56\u7565\u7c7b\u5fc5\u987b\u5b9e\u73b0\u7684\u65b9\u6cd5<\/p>\n<\/li>\n<li>\n<p>\u62bd\u8c61\u4e86&#034;\u8ffd\u6c42\u65b9\u5f0f&#034;\u8fd9\u4e00\u884c\u4e3a<\/p>\n<\/li>\n<\/ul>\n<p>2.\u5177\u4f53\u7b56\u7565\u7c7b (LikeType1\/2\/3)<\/p>\n<p>public class LikeType1 implements LikeGril {<br \/>\n    &#064;Override<br \/>\n    public void likeCategory(String type) {<\/p>\n<p>        System.out.println(&#034;\u4f7f\u7528\u65b9\u5f0f1&#xff1a;&#034;&#043;  type&#043; &#034;\u6765\u83b7\u5f97\u82b3\u5fc3&#034;);<br \/>\n    }<br \/>\n}<br \/>\npublic class LikeType2 implements LikeGril {<br \/>\n    &#064;Override<br \/>\n    public void likeCategory(String type) {<\/p>\n<p>        System.out.println(&#034;\u4f7f\u7528\u65b9\u5f0f2&#xff1a;&#034;&#043;  type&#043; &#034;\u6765\u83b7\u5f97\u82b3\u5fc3&#034;);<br \/>\n    }<br \/>\n}<br \/>\npublic class LikeType3   implements LikeGril {<br \/>\n    &#064;Override<br \/>\n    public void likeCategory(String type) {<\/p>\n<p>        System.out.println(&#034;\u4f7f\u7528\u65b9\u5f0f3&#xff1a;&#034;&#043;  type&#043; &#034;\u6765\u83b7\u5f97\u82b3\u5fc3&#034;);<br \/>\n    }<br \/>\n} <\/p>\n<ul>\n<li>\n<p>\u6bcf\u4e2a\u7c7b\u5b9e\u73b0\u4e86\u4e0d\u540c\u7684\u5177\u4f53\u7b56\u7565<\/p>\n<\/li>\n<li>\n<p>\u5c01\u88c5\u4e86\u5177\u4f53\u7684\u7b97\u6cd5\/\u884c\u4e3a\u5b9e\u73b0<\/p>\n<\/li>\n<\/ul>\n<p>3.\u7b56\u7565\u7c7b (LikeContext)<\/p>\n<p>public class LikeContext {<\/p>\n<p>    \/\/\u58f0\u660e\u7684\u63a5\u53e3\u5bf9\u8c61,\u7236\u7c7b\u5bf9\u8c61&#xff0c;\u53ef\u4ee5\u7ed1\u5b9aLikeType<br \/>\n    private  LikeGril  lg;<\/p>\n<p>    \/\/\u6784\u9020\u6ce8\u5165\u3010spring\u3011&#xff0c;\u901a\u8fc7LikeContext\u7c7b\u7684\u6784\u9020\u51fd\u6570\u7ed9\u63a5\u53e3LikeGril\u7ed1\u5b9a\u5177\u4f53\u7684\u5bf9\u8c61&#xff0c;\u5b8c\u6210\u5bf9\u8c61\u7684\u5b9e\u4f8b\u5316<br \/>\n    public LikeContext(LikeGril  lg)<br \/>\n    {<br \/>\n        this.lg &#061; lg;<br \/>\n    }<\/p>\n<p>    \/\/set\u6ce8\u5165\u3010spring\u3011,\u901a\u8fc7set\u65b9\u6cd5\u4f20\u5165\u5bf9\u8c61<br \/>\n    public  void  setLg(LikeGril  lg)<br \/>\n    {<br \/>\n        this.lg &#061; lg;<br \/>\n    }<\/p>\n<p>    \/\/\u6267\u884c\u559c\u6b22\u7684\u65b9\u6cd5<br \/>\n    public void  execCategory(String  type)<br \/>\n    {<br \/>\n        this.lg.likeCategory(type);<br \/>\n    }<\/p>\n<p>} <\/p>\n<ul>\n<li>\n<p>\u7ef4\u62a4\u5bf9\u7b56\u7565\u5bf9\u8c61\u7684\u5f15\u7528<\/p>\n<\/li>\n<li>\n<p>\u63d0\u4f9b\u5207\u6362\u7b56\u7565\u7684\u65b9\u6cd5<\/p>\n<\/li>\n<li>\n<p>\u5c06\u5ba2\u6237\u7aef\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u5f53\u524d\u7b56\u7565<\/p>\n<\/li>\n<\/ul>\n<p>4.\u5ba2\u6237\u7aef\u4f7f\u7528\u65b9\u5f0f<\/p>\n<p>package strategyDemo1;<\/p>\n<p>public class Test {<\/p>\n<p>    public static void main(String[] args) {<\/p>\n<p>        \/\/ \u521b\u5efa\u7b56\u7565\u7c7b\u5e76\u8bbe\u7f6e\u521d\u59cb\u7b56\u7565<br \/>\n        LikeContext  lc1  &#061;   new LikeContext(new LikeType2());<br \/>\n        lc1.execCategory(&#034;\u9999\u6c34&#034;);<\/p>\n<p>        \/\/ \u8fd0\u884c\u65f6\u5207\u6362\u7b56\u7565<br \/>\n        lc1.setLg(new LikeType3());<br \/>\n        lc1.execCategory(&#034;\u7f8e\u98df&#034;);<\/p>\n<p>        \/\/ \u4f7f\u7528\u4e0d\u540c\u7b56\u7565\u7684\u53e6\u4e00\u4e2a\u7b56\u7565\u7c7b\u5bf9\u8c61<br \/>\n        LikeContext  lc2  &#061;   new LikeContext(new LikeType1());<br \/>\n        lc2.execCategory(&#034;\u9c9c\u82b1&#034;);<br \/>\n    }<br \/>\n}<\/p>\n<ul>\n<li>\n<p>\u5ba2\u6237\u7aef\u51b3\u5b9a\u4f7f\u7528\u54ea\u79cd\u7b56\u7565<\/p>\n<\/li>\n<li>\n<p>\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u5207\u6362\u7b56\u7565<\/p>\n<\/li>\n<li>\n<p>\u591a\u4e2a\u7b56\u7565\u7c7b\u5bf9\u8c61\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528\u4e0d\u540c\u7b56\u7565<\/p>\n<\/li>\n<\/ul>\n<p>\u8fd0\u884c\u6548\u679c\u5982\u4e0b&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"148\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811075422-6899a1ae4ff26.png\" width=\"608\" \/><\/p>\n<h3 id=\"%E5%9F%BA%E4%BA%8E%E9%9D%A2%E5%90%91%E5%87%BD%E6%95%B0%E6%8E%A5%E5%8F%A3%E7%9A%84%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F\">\u57fa\u4e8e\u9762\u5411\u51fd\u6570\u63a5\u53e3\u7684\u7b56\u7565\u6a21\u5f0f<\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color:#4da8ee\">\u7b56\u7565\u7c7b\u4e2d\u7684\u6784\u9020\u6ce8\u5165\u6216set\u6ce8\u5165\u4f20\u5165\u7684\u53c2\u6570\u662f\u51fd\u6570&#xff08;lambda\u8868\u8fbe\u5f0f&#xff09;\u3002<\/span><\/p>\n<ul>\n<li>\u4ee3\u7801\u7b80\u6d01\u6027&#xff1a;\u7701\u53bb\u4e86\u591a\u4e2a\u5177\u4f53\u7b56\u7565\u7c7b\u7684\u5b9a\u4e49<\/li>\n<li>\u5373\u65f6\u5b9a\u4e49\u7b56\u7565&#xff1a;\u53ef\u4ee5\u5728\u8c03\u7528\u65f6\u73b0\u573a\u5b9a\u4e49\u7b56\u7565\u884c\u4e3a<\/li>\n<li>\u51fd\u6570\u5f0f\u7f16\u7a0b\u7279\u6027&#xff1a;\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ec4\u5408\u3001\u4f20\u9012\u7b56\u7565\u51fd\u6570<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u4f53\u73b0\u4e86\u57fa\u4e8e\u9762\u5411\u51fd\u6570\u63a5\u53e3\u7684\u7b56\u7565\u6a21\u5f0f\u7684\u5b9e\u73b0&#xff1a;<\/p>\n<p>1.\u51fd\u6570\u5f0f\u7b56\u7565\u63a5\u53e3<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4f7f\u7528&#064;FunctionalInterface\u6ce8\u89e3\u660e\u786e\u6807\u8bb0\u4e3a\u51fd\u6570\u5f0f\u63a5\u53e3&#xff0c;\u91cc\u9762\u53ea\u80fd\u6709\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\u3002<\/p>\n<p>&#064;FunctionalInterface<br \/>\npublic interface LikeGril {<br \/>\n    public void  likeCategory(String  type);<br \/>\n} <\/p>\n<p>2.\u7b56\u7565\u7c7b<\/p>\n<p>package strategyDemo2;<\/p>\n<p>public class User {<\/p>\n<p>    \/\/\u58f0\u660e\u7684\u63a5\u53e3\u5bf9\u8c61<br \/>\n    private LikeGril lg;<\/p>\n<p>    \/\/ \u6784\u9020\u6ce8\u5165<br \/>\n    public User(LikeGril lg)<br \/>\n    {<br \/>\n        this.lg &#061; lg;<br \/>\n    }<\/p>\n<p>    \/\/ \u6267\u884c\u7b56\u7565<br \/>\n    public void execCategory(String  type)<br \/>\n    {<br \/>\n        this.lg.likeCategory(type);<br \/>\n    }<\/p>\n<p> }<\/p>\n<p>3.\u5ba2\u6237\u7aef\u4f7f\u7528Lambda\u8868\u8fbe\u5f0f<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u76f4\u63a5\u4f7f\u7528Lambda\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u5177\u4f53\u7b56\u7565\u5b9e\u73b0&#xff0c;\u6bcf\u4e2aLambda\u76f8\u5f53\u4e8e\u4e00\u4e2a\u7b56\u7565\u5b9e\u73b0\u7c7b&#xff0c;\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u76f4\u89c2\u3002<\/p>\n<p> public static void main(String[] args) {<\/p>\n<p>        User  u1  &#061; new User((type)-&gt;{ System.out.println(&#034;\u559c\u6b22\u7684\u65b9\u5f0f\u67091:&#034;&#043;type&#043;&#034;&#xff0c;\u6765\u83b7\u53d6\u82b3\u5fc3&#034;); });<br \/>\n        u1.execCategory(&#034;\u9c9c\u82b1&#034;);<\/p>\n<p>        User  u2  &#061; new User((type)-&gt;{ System.out.println(&#034;\u559c\u6b22\u7684\u65b9\u5f0f\u67092:&#034;&#043;type&#043;&#034;&#xff0c;\u6765\u83b7\u53d6\u82b3\u5fc3&#034;); });<br \/>\n        u2.execCategory(&#034;\u7f8e\u98df&#034;);<br \/>\n    } <\/p>\n<p>\u8fd0\u884c\u6548\u679c\u5982\u4e0b&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"128\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811075422-6899a1ae6ac7f.png\" width=\"586\" \/><\/p>\n<\/p>\n<h2 id=\"%E4%BB%A3%E7%90%86%E6%A8%A1%E5%BC%8F\"><span style=\"color:#fe2c24\">\u4ee3\u7406\u6a21\u5f0f<\/span><\/h2>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u4ee3\u7406\u6a21\u5f0f\u662f\u5207\u9762\u7f16\u7a0b\u7684\u6838\u5fc3\u6a21\u5f0f\u3002\u5207\u9762&#xff1a; \u6267\u884c\u8fd9\u4e2a\u65b9\u6cd5\u4e4b\u524d\u548c\u4e4b\u540e\u505a\u7684\u4e1a\u52a1\u903b\u8f91\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color:#4da8ee\">\u00a0\u63cf\u8ff0&#xff1a;\u4ee3\u7406\u7c7b\u4ee3\u7406\u771f\u5b9e\u7684\u4e1a\u52a1\u7c7b\u5f62\u6210\u5207\u9762\u7c7b\u5bf9\u771f\u5b9e\u7684\u4e1a\u52a1\u7c7b\u7684\u62e6\u622a\u3002<\/span><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u524d\u7f6e\u62e6\u622a&#xff1a;\u9274\u6743&#xff0c;\u53cd\u722c\u866b\u673a\u5236&#xff0c; \u8bbe\u5907\u7684\u7c7b\u578b\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u540e\u7f6e\u62e6\u622a&#xff1a;\u4e00\u4e2a\u65b9\u6cd5\u7684\u6027\u80fd\u7684\u6570\u636e\u91c7\u96c6\u3002<\/p>\n<h3 id=\"%C2%A0%20%E9%9D%99%E6%80%81%E4%BB%A3%E7%90%86\">\u00a0 \u9759\u6001\u4ee3\u7406<\/h3>\n<p>\u4f18\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u804c\u8d23\u6e05\u6670&#xff1a;\u771f\u5b9e\u5bf9\u8c61\u53ea\u9700\u5173\u6ce8\u6838\u5fc3\u4e1a\u52a1<\/p>\n<\/li>\n<li>\n<p>\u6269\u5c55\u6027\u5f3a&#xff1a;\u901a\u8fc7\u4ee3\u7406\u53ef\u4ee5\u65b9\u4fbf\u5730\u6dfb\u52a0\u989d\u5916\u529f\u80fd<\/p>\n<\/li>\n<li>\n<p>\u4fdd\u62a4\u771f\u5b9e\u5bf9\u8c61&#xff1a;\u5ba2\u6237\u7aef\u4e0d\u76f4\u63a5\u8bbf\u95ee\u771f\u5b9e\u5bf9\u8c61<\/p>\n<\/li>\n<\/ul>\n<p>\u7f3a\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6bcf\u4e2a\u771f\u5b9e\u7c7b\u90fd\u9700\u8981\u4e00\u4e2a\u4ee3\u7406\u7c7b<\/p>\n<\/li>\n<li>\n<p>\u63a5\u53e3\u53d8\u5316\u65f6&#xff0c;\u4ee3\u7406\u7c7b\u548c\u771f\u5b9e\u7c7b\u90fd\u9700\u8981\u4fee\u6539<\/p>\n<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u901a\u8fc7\u4ee3\u7801\u6765\u4f53\u73b0\u9759\u6001\u4ee3\u7406\u6a21\u5f0f\u7684\u5b9e\u73b0&#xff1a;<\/p>\n<p>1.\u5b9a\u4e49\u63a5\u53e3<\/p>\n<p>public interface Operator {<br \/>\n    public  void  operatorHouse();<br \/>\n} <\/p>\n<p>\u4ee3\u7406\u7c7bProxy\u548c\u771f\u5b9e\u7c7bStuOperator\u5b9e\u73b0\u76f8\u540c\u63a5\u53e3\u3002<\/p>\n<p>2.\u771f\u5b9e\u7c7b&#xff1a;<\/p>\n<p>\/\/\u5b66\u751f\u7c7b  \u771f\u5b9e\u7c7b<br \/>\npublic class StuOperator implements Operator {<br \/>\n    &#064;Override<br \/>\n    public void operatorHouse() {<br \/>\n        System.out.println(&#034;\u5b66\u751f\u60f3\u79df\u6709\u653f\u5e9c\u8865\u8d34\u7684\u516c\u79df\u623f&#034;);<br \/>\n    }<br \/>\n} <\/p>\n<p>3.\u4ee3\u7406\u7c7b&#xff1a;<\/p>\n<p>\/\/\u4ee3\u7406\u7c7b&#xff0c;\u4e2d\u4ecb&#xff0c;\u4ee3\u7406\u5b66\u751f\u53bb\u79df\u623f\u5b50<br \/>\npublic class Proxy implements  Operator{<\/p>\n<p>    private Operator op;   \/\/\u4ee3\u7406\u6301\u6709\u771f\u5b9e\u5bf9\u8c61\u7684\u5f15\u7528<\/p>\n<p>    private Interceptor in;<\/p>\n<p>    \/\/ \u6784\u9020\u6ce8\u5165&#xff0c;\u771f\u5b9e\u7c7b\u4f20\u7ed9op&#xff0c;\u62e6\u622a\u7c7b\u4f20\u7ed9in<br \/>\n    public Proxy(Operator op,Interceptor in)<br \/>\n    {<br \/>\n        this.op &#061; op;<br \/>\n        this.in &#061; in;<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public void operatorHouse() {<\/p>\n<p>        \/\/\u4ee3\u7406\u63a7\u5236\u5bf9\u771f\u5b9e\u5bf9\u8c61\u7684\u8bbf\u95ee<br \/>\n        \/\/\u524d\u7f6e\u62e6\u622a<br \/>\n        this.in.beforeAction();<br \/>\n        \/\/\u79df\u623f\u5b50<br \/>\n        this.op.operatorHouse();         \/\/op\u4f20\u5165\u7684\u5bf9\u8c61\u7c7b\u578b\u7684\u53c2\u6570\u662fnew StuOperator(),\u6240\u4ee5\u76f8\u5f53\u4e8eStuOperator.operatorHouse()<br \/>\n        \/\/\u540e\u7f6e\u62e6\u622a<br \/>\n        this.in.afterAction();<\/p>\n<p>    }<br \/>\n} <\/p>\n<p>4.\u62e6\u622a\u7684\u63a5\u53e3\u4e0e\u5177\u4f53\u7684\u5b9e\u73b0\u7c7b&#xff1a;<\/p>\n<p>\/\/\u62e6\u622a\u7684\u63a5\u53e3<br \/>\npublic interface Interceptor {<br \/>\n    public  void  beforeAction();<br \/>\n    public   void  afterAction();<br \/>\n}<\/p>\n<p>public class HouseInterceptor  implements   Interceptor {<br \/>\n    &#064;Override<br \/>\n    public void beforeAction() {<br \/>\n        System.out.println(&#034;\u68c0\u67e5\u60a8\u7684\u8eab\u4efd\u8bc1\u548c\u653f\u5e9c\u7684\u6587\u4ef6&#034;);<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public void afterAction() {<br \/>\n        System.out.println(&#034;\u68c0\u67e5\u60a8\u79df\u623f\u5b50\u7684\u8bbe\u5907\u548c\u5176\u5b83\u88c5\u4fee\u7684\u60c5\u51b5&#034;);<\/p>\n<p>    }<br \/>\n} <\/p>\n<p>5.\u5ba2\u6237\u7aef\u4ee3\u7801&#xff1a;<\/p>\n<p>package proxyDemo1;<\/p>\n<p>public class Test {<\/p>\n<p>    public static void main(String[] args) {<\/p>\n<p>        \/\/\u5ba2\u6237\u7aef\u521b\u5efa\u4ee3\u7406\u5bf9\u8c61&#xff0c;\u4f20\u5165\u771f\u5b9e\u5bf9\u8c61\u548c\u62e6\u622a\u5668<br \/>\n        Proxy  proxy   &#061; new Proxy(new StuOperator(),new HouseInterceptor());   \/\/\u4f20\u5165\u5bf9\u8c61\u7c7b\u578b\u7684\u53c2\u6570&#xff08;\u771f\u5b9e\u7c7b&#xff0c;\u62e6\u622a\u7c7b&#xff09;<\/p>\n<p>        \/\/\u5ba2\u6237\u7aef\u901a\u8fc7\u4ee3\u7406\u95f4\u63a5\u8bbf\u95ee\u771f\u5b9e\u5bf9\u8c61<br \/>\n        proxy.operatorHouse();<\/p>\n<p>    }<br \/>\n} <\/p>\n<p>\u00a06.\u9759\u6001\u4ee3\u7406\u7684\u6267\u884c\u6d41\u7a0b<\/p>\n<p>&#xff08;1&#xff09;\u5ba2\u6237\u7aef\u521b\u5efa\u4ee3\u7406\u5bf9\u8c61&#xff0c;\u4f20\u5165\u771f\u5b9e\u5bf9\u8c61\u548c\u62e6\u622a\u5668<\/p>\n<p> Proxy  proxy   &#061; new Proxy(new StuOperator(),new HouseInterceptor()); <\/p>\n<p>&#xff08;2&#xff09;\u5ba2\u6237\u7aef\u8c03\u7528\u4ee3\u7406\u65b9\u6cd5<\/p>\n<p> proxy.operatorHouse(); <\/p>\n<p>&#xff08;3&#xff09;\u4ee3\u7406\u6267\u884c\u6d41\u7a0b&#xff1a;<\/p>\n<p>\/\/\u901a\u8fc7\u52a8\u6001\u7ed1\u5b9a<br \/>\n1. \u8c03\u7528\u62e6\u622a\u5668beforeAction()<br \/>\n2. \u8c03\u7528\u771f\u5b9e\u5bf9\u8c61\u7684operatorHouse()<br \/>\n3. \u8c03\u7528\u62e6\u622a\u5668afterAction() <\/p>\n<p>\u8fd0\u884c\u6548\u679c\u5982\u4e0b&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"146\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811075422-6899a1ae8bbb0.png\" width=\"603\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb235\u6b21\uff0c\u70b9\u8d5e3\u6b21\uff0c\u6536\u85cf3\u6b21\u3002\u4f53\u73b0\u70b9\u5b9a\u4e49\u4e86\u521b\u5efa&quot;\u4ea7\u54c1\u65cf&quot;\u7684\u63a5\u53e3\uff08\u52a8\u7269\u548c\u98df\u7269\uff09\u4e0d\u5173\u5fc3\u5177\u4f53\u5b9e\u73b0\uff0c\u53ea\u58f0\u660e\u80fd\u521b\u5efa\u54ea\u4e9b\u7c7b\u578b\u7684\u4ea7\u54c1\u7b26\u5408&quot;\u62bd\u8c61\u5de5\u5382&quot;\u4e2d&quot;\u62bd\u8c61&quot;\u7684\u6982\u5ff5\u5728\u62bd\u8c61\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u6a21\u677f\u65b9\u6cd5\uff08\u6267\u884c\u6d41\u7a0b\u4e0d\u5e0c\u671b\u88ab\u6539\u53d8\uff0c\u7528final\u5173\u952e\u5b57\u4fee\u9970\uff0c\u8be5\u65b9\u6cd5\u4e0d\u88ab\u5b50\u7c7b\u7ee7\u627f\uff09\u3002\/\/\u5b9a\u4e49\u4e00\u4e2a\u6a21\u677f\u65b9\u6cd5\uff0c\u7528final\u5173\u952e\u5b57\u4fee\u9970\uff0c\u8be5\u65b9\u6cd5\u4e0d\u88ab\u5b50\u7c7b\u7ee7\u627fSystem.out.println(&quot;\u5de5\u4f5c\u5230\u70b9\u4e0b\u73ed&quot;);\/\/\u56de\u5bb6\u7684\u4ea4\u901a\u5de5\u5177\u4e0d\u4e00\u6837\uff0c\u62bd\u8c61\u65b9\u6cd5\uff0c\u7531\u5b50\u7c7b\u5b9e\u73b0System.out.println(&quot;\u5230\u5bb6\u4e86&quot;);<\/p>\n","protected":false},"author":2,"featured_media":52539,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,190,2874],"topic":[],"class_list":["post-52549","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-java","tag-190","tag-2874"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java\u8bbe\u8ba1\u6a21\u5f0f\uff08\u4e00\uff09 - \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\/52549.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java\u8bbe\u8ba1\u6a21\u5f0f\uff08\u4e00\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb235\u6b21\uff0c\u70b9\u8d5e3\u6b21\uff0c\u6536\u85cf3\u6b21\u3002\u4f53\u73b0\u70b9\u5b9a\u4e49\u4e86\u521b\u5efa&quot;\u4ea7\u54c1\u65cf&quot;\u7684\u63a5\u53e3\uff08\u52a8\u7269\u548c\u98df\u7269\uff09\u4e0d\u5173\u5fc3\u5177\u4f53\u5b9e\u73b0\uff0c\u53ea\u58f0\u660e\u80fd\u521b\u5efa\u54ea\u4e9b\u7c7b\u578b\u7684\u4ea7\u54c1\u7b26\u5408&quot;\u62bd\u8c61\u5de5\u5382&quot;\u4e2d&quot;\u62bd\u8c61&quot;\u7684\u6982\u5ff5\u5728\u62bd\u8c61\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u6a21\u677f\u65b9\u6cd5\uff08\u6267\u884c\u6d41\u7a0b\u4e0d\u5e0c\u671b\u88ab\u6539\u53d8\uff0c\u7528final\u5173\u952e\u5b57\u4fee\u9970\uff0c\u8be5\u65b9\u6cd5\u4e0d\u88ab\u5b50\u7c7b\u7ee7\u627f\uff09\u3002\/\/\u5b9a\u4e49\u4e00\u4e2a\u6a21\u677f\u65b9\u6cd5\uff0c\u7528final\u5173\u952e\u5b57\u4fee\u9970\uff0c\u8be5\u65b9\u6cd5\u4e0d\u88ab\u5b50\u7c7b\u7ee7\u627fSystem.out.println(&quot;\u5de5\u4f5c\u5230\u70b9\u4e0b\u73ed&quot;);\/\/\u56de\u5bb6\u7684\u4ea4\u901a\u5de5\u5177\u4e0d\u4e00\u6837\uff0c\u62bd\u8c61\u65b9\u6cd5\uff0c\u7531\u5b50\u7c7b\u5b9e\u73b0System.out.println(&quot;\u5230\u5bb6\u4e86&quot;);\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/52549.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-11T07:54:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811075419-6899a1ab9f3d7.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=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/52549.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/52549.html\",\"name\":\"Java\u8bbe\u8ba1\u6a21\u5f0f\uff08\u4e00\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-11T07:54:23+00:00\",\"dateModified\":\"2025-08-11T07:54:23+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/52549.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/52549.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/52549.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\u8bbe\u8ba1\u6a21\u5f0f\uff08\u4e00\uff09\"}]},{\"@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":"Java\u8bbe\u8ba1\u6a21\u5f0f\uff08\u4e00\uff09 - \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\/52549.html","og_locale":"zh_CN","og_type":"article","og_title":"Java\u8bbe\u8ba1\u6a21\u5f0f\uff08\u4e00\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb235\u6b21\uff0c\u70b9\u8d5e3\u6b21\uff0c\u6536\u85cf3\u6b21\u3002\u4f53\u73b0\u70b9\u5b9a\u4e49\u4e86\u521b\u5efa&quot;\u4ea7\u54c1\u65cf&quot;\u7684\u63a5\u53e3\uff08\u52a8\u7269\u548c\u98df\u7269\uff09\u4e0d\u5173\u5fc3\u5177\u4f53\u5b9e\u73b0\uff0c\u53ea\u58f0\u660e\u80fd\u521b\u5efa\u54ea\u4e9b\u7c7b\u578b\u7684\u4ea7\u54c1\u7b26\u5408&quot;\u62bd\u8c61\u5de5\u5382&quot;\u4e2d&quot;\u62bd\u8c61&quot;\u7684\u6982\u5ff5\u5728\u62bd\u8c61\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u6a21\u677f\u65b9\u6cd5\uff08\u6267\u884c\u6d41\u7a0b\u4e0d\u5e0c\u671b\u88ab\u6539\u53d8\uff0c\u7528final\u5173\u952e\u5b57\u4fee\u9970\uff0c\u8be5\u65b9\u6cd5\u4e0d\u88ab\u5b50\u7c7b\u7ee7\u627f\uff09\u3002\/\/\u5b9a\u4e49\u4e00\u4e2a\u6a21\u677f\u65b9\u6cd5\uff0c\u7528final\u5173\u952e\u5b57\u4fee\u9970\uff0c\u8be5\u65b9\u6cd5\u4e0d\u88ab\u5b50\u7c7b\u7ee7\u627fSystem.out.println(&quot;\u5de5\u4f5c\u5230\u70b9\u4e0b\u73ed&quot;);\/\/\u56de\u5bb6\u7684\u4ea4\u901a\u5de5\u5177\u4e0d\u4e00\u6837\uff0c\u62bd\u8c61\u65b9\u6cd5\uff0c\u7531\u5b50\u7c7b\u5b9e\u73b0System.out.println(&quot;\u5230\u5bb6\u4e86&quot;);","og_url":"https:\/\/www.wsisp.com\/helps\/52549.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-11T07:54:23+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811075419-6899a1ab9f3d7.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/52549.html","url":"https:\/\/www.wsisp.com\/helps\/52549.html","name":"Java\u8bbe\u8ba1\u6a21\u5f0f\uff08\u4e00\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-11T07:54:23+00:00","dateModified":"2025-08-11T07:54:23+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/52549.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/52549.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/52549.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Java\u8bbe\u8ba1\u6a21\u5f0f\uff08\u4e00\uff09"}]},{"@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\/52549","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=52549"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/52549\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/52539"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=52549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=52549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=52549"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=52549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}