{"id":202583,"date":"2025-05-22T10:56:18","date_gmt":"2025-05-22T02:56:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202583\/"},"modified":"2025-05-22T10:56:18","modified_gmt":"2025-05-22T02:56:18","slug":"%e7%bc%96%e5%86%99%e5%8f%af%e9%87%8d%e7%94%a8-java-%e5%87%bd%e6%95%b0%e7%9a%84%e9%80%9a%e7%94%a8%e5%8e%9f%e5%88%99%e5%92%8c%e5%87%86%e5%88%99","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202583\/","title":{"rendered":"\u7f16\u5199\u53ef\u91cd\u7528 Java \u51fd\u6570\u7684\u901a\u7528\u539f\u5219\u548c\u51c6\u5219"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u7f16\u5199\u53ef\u91cd\u7528 Java \u51fd\u6570\u7684\u901a\u7528\u539f\u5219\u548c\u51c6\u5219<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u7f16\u5199\u53ef\u91cd\u7528 Java \u51fd\u6570\u7684\u901a\u7528\u539f\u5219\u548c\u51c6\u5219\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p>\u53ef\u91cd\u7528 Java \u51fd\u6570\u7684\u539f\u5219\u548c\u51c6\u5219\u5305\u62ec\uff1a\u5355\u4e00\u804c\u8d23\u539f\u5219\uff1a\u6bcf\u4e2a\u51fd\u6570\u53ea\u8d1f\u8d23\u4e00\u4e2a\u660e\u786e\u7684\u4efb\u52a1\u3002DRY \u539f\u5219\uff1a\u907f\u514d\u91cd\u590d\u76f8\u4f3c\u7684\u903b\u8f91\uff0c\u5c06\u5171\u4eab\u903b\u8f91\u63d0\u53d6\u5230\u5355\u72ec\u7684\u51fd\u6570\u6216\u7c7b\u4e2d\u3002\u9ad8\u5185\u805a\uff0c\u4f4e\u8026\u5408\uff1a\u5c06\u76f8\u5173\u4ee3\u7801\u7ec4\u4ef6\u5206\u7ec4\uff0c\u964d\u4f4e\u4f9d\u8d56\u6027\u3002\u63a5\u53e3\u548c\u62bd\u8c61\u7c7b\uff1a\u5b9a\u4e49\u51fd\u6570\u7684\u516c\u5171\u5951\u7ea6\uff0c\u800c\u65e0\u9700\u6307\u5b9a\u5177\u4f53\u5b9e\u73b0\u3002\u4f7f\u7528 JavaDoc \u6ce8\u91ca\u51fd\u6570\u7684\u8f93\u5165\u3001\u8f93\u51fa\u3001\u5f02\u5e38\u548c\u7528\u9014\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729920031671c7c1f5a397.jpg\" class=\"aligncenter\"><\/p>\n<h2>\u7f16\u5199\u53ef\u91cd\u7528 Java \u51fd\u6570\u7684\u539f\u5219\u548c\u51c6\u5219<\/h2>\n<p>\u7f16\u5199\u53ef\u91cd\u7528\u7684\u4ee3\u7801\u662f Java \u5f00\u53d1\u4e2d\u7684\u5173\u952e\u5b9e\u8df5\uff0c\u5b83\u53ef\u4ee5\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3001\u53ef\u8bfb\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002\u672c\u6587\u5c06\u8ba8\u8bba\u7f16\u5199\u53ef\u91cd\u7528 Java \u51fd\u6570\u7684\u901a\u7528\u539f\u5219\u548c\u51c6\u5219\uff0c\u5e76\u63d0\u4f9b\u5b9e\u9645\u6848\u4f8b\u6765\u6f14\u793a\u8fd9\u4e9b\u539f\u5219\u7684\u5e94\u7528\u3002<\/p>\n<h3>\u5355\u4e00\u804c\u8d23\u539f\u5219<\/h3>\n<p>\u6bcf\u200b\u200b\u4e2a\u51fd\u6570\u90fd\u5e94\u8be5\u53ea\u6267\u884c\u4e00\u4e2a\u3001\u5b9a\u4e49\u660e\u786e\u7684\u4efb\u52a1\u3002\u8fd9\u6709\u52a9\u4e8e\u964d\u4f4e\u8026\u5408\u5ea6\uff0c\u5e76\u4f7f\u51fd\u6570\u66f4\u5bb9\u6613\u7406\u89e3\u548c\u6d4b\u8bd5\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>public String formatPhoneNumber(String phoneNumber) {\n  \/\/ \u89c4\u8303\u5316\u7535\u8bdd\u53f7\u7801\uff0c\u79fb\u9664\u6240\u6709\u7279\u6b8a\u5b57\u7b26\n  String normalizedPhoneNumber = phoneNumber.replaceAll(\"[^0-9]\", \"\");\n  \/\/ \u6839\u636e\u7ed9\u5b9a\u7684\u683c\u5f0f\u683c\u5f0f\u5316\u7535\u8bdd\u53f7\u7801\n  return String.format(\"(%s) %s-%s\", normalizedPhoneNumber.substring(0, 3), normalizedPhoneNumber.substring(3, 6), normalizedPhoneNumber.substring(6));\n}<\/pre>\n<h3>DRY \u539f\u5219\uff08\u4e0d\u8981\u91cd\u590d\u81ea\u5df1\uff09<\/h3>\n<p>\u907f\u514d\u5728\u4ee3\u7801\u4e2d\u91cd\u590d\u76f8\u540c\u6216\u76f8\u4f3c\u7684\u903b\u8f91\u3002\u63d0\u53d6\u5171\u4eab\u903b\u8f91\u5230\u5355\u72ec\u7684\u51fd\u6570\u6216\u7c7b\u4e2d\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u63d0\u53d6\u5728\u591a\u4e2a\u51fd\u6570\u4e2d\u4f7f\u7528\u7684\u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u7684\u903b\u8f91\npublic boolean isValidEmailAddress(String emailAddress) {\n  return emailAddress.matches(\"^[\\\\w-_.+]*@[\\\\w-_.+]*\\\\.[\\\\w-_.+]*$\");\n}<\/pre>\n<h3>\u9ad8\u5185\u805a\uff0c\u4f4e\u8026\u5408<\/h3>\n<p>\u5c06\u76f8\u5173\u7684\u4ee3\u7801\u7ec4\u4ef6\u7ec4\u5408\u5230\u6a21\u5757\u6216\u5b50\u7cfb\u7edf\u4e2d\uff0c\u4ee5\u964d\u4f4e\u76f8\u4e92\u4f9d\u8d56\u6027\u3002\u8fd9\u4f7f\u51fd\u6570\u66f4\u6613\u4e8e\u7406\u89e3\u3001\u66f4\u6539\u548c\u7ef4\u62a4\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u5c06\u4e0e\u7528\u6237\u9a8c\u8bc1\u76f8\u5173\u7684\u51fd\u6570\u5206\u7ec4\u5230\u4e00\u4e2a\u6a21\u5757\u4e2d\npublic class UserValidationModule {\n\n  public static boolean isValidUsername(String username) {\n    \/\/ \u9a8c\u8bc1\u7528\u6237\u540d\u662f\u5426\u6709\u6548\n  }\n\n  public static boolean isValidPassword(String password) {\n    \/\/ \u9a8c\u8bc1\u5bc6\u7801\u662f\u5426\u6709\u6548\n  }\n}<\/pre>\n<h3>\u63a5\u53e3\u548c\u62bd\u8c61\u7c7b<\/h3>\n<p>\u4f7f\u7528\u63a5\u53e3\u548c\u62bd\u8c61\u7c7b\u5b9a\u4e49\u51fd\u6570\u7684\u516c\u5171\u5951\u7ea6\uff0c\u800c\u65e0\u9700\u6307\u5b9a\u5177\u4f53\u5b9e\u73b0\u3002\u8fd9\u4f7f\u60a8\u53ef\u4ee5\u5728\u4e0d\u7834\u574f\u73b0\u6709\u5ba2\u6237\u7684\u60c5\u51b5\u4e0b\u4ea4\u6362\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u5b9a\u4e49\u4e00\u4e2a\u63a5\u53e3\u6765\u8868\u793a\u6392\u5e8f\u7b97\u6cd5\npublic interface SortingAlgorithm {\n  int[] sort(int[] numbers);\n}\n\n\/\/ \u63d0\u4f9b\u5177\u4f53\u5b9e\u73b0\u7684\u5177\u4f53\u7c7b\npublic class BubbleSort implements SortingAlgorithm {\n  @Override\n  public int[] sort(int[] numbers) {\n    \/\/ \u5b9e\u73b0\u5192\u6ce1\u6392\u5e8f\u7b97\u6cd5\n  }\n}<\/pre>\n<h3>\u4f7f\u7528 JavaDoc<\/h3>\n<p>\u4f7f\u7528 JavaDoc \u6ce8\u91ca\u6765\u8bb0\u5f55\u51fd\u6570\u7684\u8f93\u5165\u3001\u8f93\u51fa\u3001\u5f02\u5e38\u548c\u7528\u9014\u3002\u8fd9\u4f7f\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u66f4\u8f7b\u677e\u5730\u7406\u89e3\u548c\u4f7f\u7528\u5e93\u6216\u7b2c\u4e09\u65b9\u4ee3\u7801\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/**\n * \u683c\u5f0f\u5316\u7535\u8bdd\u53f7\u7801\n *\n * @param phoneNumber \u7535\u8bdd\u53f7\u7801\n * @return \u683c\u5f0f\u5316\u7684\u7535\u8bdd\u53f7\u7801\n *\/\npublic String formatPhoneNumber(String phoneNumber) {\n  \/\/ ...\n}<\/pre>\n<h3>\u5b9e\u6218\u6848\u4f8b<\/h3>\n<p>\u4f7f\u7528\u4e0a\u8ff0\u539f\u5219\u548c\u51c6\u5219\uff0c\u8ba9\u6211\u4eec\u8003\u8651\u4e00\u4e2a\u53ef\u91cd\u7528\u51fd\u6570\u7684\u5b9e\u9645\u6848\u4f8b\uff0c\u7528\u4e8e\u5c06\u6570\u5b57\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5217\u8868\uff1a<\/p>\n<pre>import java.util.Arrays;\nimport java.util.List;\nimport java.util.stream.Collectors;\n\npublic class NumberToStringConverter {\n\n  public static List&lt;String&gt; convertNumbersToStrings(List&lt;Integer&gt; numbers) {\n    return numbers.stream()\n        .map(Object::toString)\n        .collect(Collectors.toList());\n  }\n}<\/pre>\n<p>\u6b64\u51fd\u6570\u9075\u5faa\u5355\u4e00\u804c\u8d23\u539f\u5219\uff0c\u56e0\u4e3a\u5b83\u53ea\u8d1f\u8d23\u5c06\u6570\u5b57\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5217\u8868\u3002\u5b83\u8fd8\u907f\u514d\u4e86\u91cd\u590d\u7684\u4ee3\u7801\uff0c\u56e0\u4e3a\u5b83\u4f7f\u7528\u4e86 Java 8 \u7684\u6d41 API \u6765\u6267\u884c\u8f6c\u6362\u3002\u8be5\u51fd\u6570\u5185\u805a\u5ea6\u9ad8\uff0c\u4e0e\u4efb\u4f55\u7279\u5b9a\u96c6\u5408\u5b9e\u73b0\u65e0\u5173\uff0c\u5e76\u4e14\u4f7f\u7528 JavaDoc \u8bb0\u5f55\u826f\u597d\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u7f16\u5199\u53ef\u91cd\u7528 Java \u51fd\u6570\u7684\u901a\u7528\u539f\u5219\u548c\u51c6\u5219\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f16\u5199\u53ef\u91cd\u7528 Java \u51fd\u6570\u7684\u901a\u7528&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202583","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202583"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202583\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}