{"id":890,"date":"2022-10-12T23:01:48","date_gmt":"2022-10-12T15:01:48","guid":{"rendered":"https:\/\/qaqaq.top\/?p=890"},"modified":"2022-11-27T12:39:53","modified_gmt":"2022-11-27T04:39:53","slug":"%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e4%b8%8b-%e6%a8%a1%e6%9d%bf%e6%96%b9%e6%b3%95%e7%9a%84%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e5%8f%8a%e5%ba%94%e7%94%a8%e5%9c%ba%e6%99%af","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=890","title":{"rendered":"\u9762\u5411\u5bf9\u8c61(\u4e0b)-\u6a21\u677f\u65b9\u6cd5\u7684\u8bbe\u8ba1\u6a21\u5f0f\u53ca\u5e94\u7528\u573a\u666f"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P348;\r\n\/*\r\n * \u62bd\u8c61\u7c7b\u7684\u5e94\u7528\uff1a\u6a21\u677f\u65b9\u6cd5\u7684\u8bbe\u8ba1\u6a21\u5f0f\r\n * \r\n * \r\n * \r\n *\/\r\npublic class TemplateTest {\r\n\t\r\n\tpublic static void main(String&#91;] args) {\r\n\t\t\r\n\t\tTemplate t = new subTemplate();\r\n\t\t\r\n\t\tt.spendTime();\r\n\t}\r\n\r\n}\r\n\r\nabstract class Template{\r\n\t\r\n\t\/\/\u8ba1\u7b97\u67d0\u6bb5\u4ee3\u7801\u6267\u884c\r\n\tpublic void spendTime() {\r\n\t\t\r\n\t\tlong start = System.currentTimeMillis();\r\n\t\t\r\n\t\tcode();\/\/\u4e0d\u786e\u5b9a\u7684\u90e8\u5206\u3001\u6613\u53d8\u7684\u90e8\u5206\r\n\t\t\r\n\t\tlong end = System.currentTimeMillis();\r\n\t\t\r\n\t\tSystem.out.println(\"\u82b1\u8d39\u7684\u65f6\u95f4\u4e3a\uff1a\" + (end - start));\r\n\t\t\r\n\t\t\r\n\t}\r\n\t\r\n\tpublic abstract void code();\r\n}\r\n\r\nclass subTemplate extends Template{\r\n\r\n\t@Override\r\n\tpublic void code() {\r\n\r\n\t\tfor(int i = 2; i &lt;= 1000; i++) {\r\n\t\t\tboolean isFlag = true;\r\n\t\t\tfor(int j = 2; j &lt;= Math.sqrt(i); j++) {\r\n\t\t\t\tif(i % j == 0) {\r\n\t\t\t\t\tisFlag = false;\r\n\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tif(isFlag) {\r\n\t\t\t\tSystem.out.println(i);\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t}\r\n\t\r\n\t\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P348;\r\n\/\/\u62bd\u8c61\u7c7b\u7684\u5e94\u7528\uff1a\u6a21\u677f\u65b9\u6cd5\u7684\u8bbe\u8ba1\u6a21\u5f0f\r\npublic class TemplateMethodTest {\r\n\r\n\tpublic static void main(String&#91;] args) {\r\n\t\tBankTemplateMethod btm = new DrawMoney();\r\n\t\tbtm.process();\r\n\r\n\t\tBankTemplateMethod btm2 = new ManageMoney();\r\n\t\tbtm2.process();\r\n\t}\r\n}\r\nabstract class BankTemplateMethod {\r\n\t\/\/ \u5177\u4f53\u65b9\u6cd5\r\n\tpublic void takeNumber() {\r\n\t\tSystem.out.println(\"\u53d6\u53f7\u6392\u961f\");\r\n\t}\r\n\r\n\tpublic abstract void transact(); \/\/ \u529e\u7406\u5177\u4f53\u7684\u4e1a\u52a1 \/\/\u94a9\u5b50\u65b9\u6cd5\r\n\r\n\tpublic void evaluate() {\r\n\t\tSystem.out.println(\"\u53cd\u9988\u8bc4\u5206\");\r\n\t}\r\n\r\n\t\/\/ \u6a21\u677f\u65b9\u6cd5\uff0c\u628a\u57fa\u672c\u64cd\u4f5c\u7ec4\u5408\u5230\u4e00\u8d77\uff0c\u5b50\u7c7b\u4e00\u822c\u4e0d\u80fd\u91cd\u5199\r\n\tpublic final void process() {\r\n\t\tthis.takeNumber();\r\n\r\n\t\tthis.transact();\/\/ \u50cf\u4e2a\u94a9\u5b50\uff0c\u5177\u4f53\u6267\u884c\u65f6\uff0c\u6302\u54ea\u4e2a\u5b50\u7c7b\uff0c\u5c31\u6267\u884c\u54ea\u4e2a\u5b50\u7c7b\u7684\u5b9e\u73b0\u4ee3\u7801\r\n\r\n\t\tthis.evaluate();\r\n\t}\r\n}\r\n\r\nclass DrawMoney extends BankTemplateMethod {\r\n\tpublic void transact() {\r\n\t\tSystem.out.println(\"\u6211\u8981\u53d6\u6b3e\uff01\uff01\uff01\");\r\n\t}\r\n}\r\n\r\nclass ManageMoney extends BankTemplateMethod {\r\n\tpublic void transact() {\r\n\t\tSystem.out.println(\"\u6211\u8981\u7406\u8d22\uff01\u6211\u8fd9\u91cc\u67092000\u4e07\u7f8e\u5143!!\");\r\n\t}\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-890","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/890"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=890"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/890\/revisions"}],"predecessor-version":[{"id":891,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/890\/revisions\/891"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}