{"id":203500,"date":"2025-05-22T14:00:33","date_gmt":"2025-05-22T06:00:33","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203500\/"},"modified":"2025-05-22T14:00:33","modified_gmt":"2025-05-22T06:00:33","slug":"java-%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bc%98%e5%8c%96%e9%80%92%e8%bf%9b%e5%bc%8f%e5%88%a4%e6%96%ad%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203500\/","title":{"rendered":"Java \u4e2d\u5982\u4f55\u4f18\u5316\u9012\u8fdb\u5f0f\u5224\u65ad\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java \u4e2d\u5982\u4f55\u4f18\u5316\u9012\u8fdb\u5f0f\u5224\u65ad\uff1f<\/h1>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJava \u4e2d\u5982\u4f55\u4f18\u5316\u9012\u8fdb\u5f0f\u5224\u65ad\uff1f\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241110\/173120224267300cc22ddc6.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>java\u4e2d\u9012\u8fdb\u5f0f\u5224\u65ad\u5982\u4f55\u4f18\u5316\uff1f<\/strong><\/p>\n<p>\u5728\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u4e86\u5d4c\u5957\u7684 if-else \u8bed\u53e5\u6765\u5224\u65ad\u56db\u4e2a\u5b57\u7b26\u4e32\u53d8\u91cf\u662f\u5426\u4e3a\u7a7a\u3002\u5f53\u53d8\u91cf\u6570\u91cf\u8f83\u591a\u65f6\uff0c\u8fd9\u79cd\u9012\u8fdb\u5f0f\u5224\u65ad\u65b9\u5f0f\u4f1a\u53d8\u5f97\u5197\u957f\u4e14\u96be\u4ee5\u7ef4\u62a4\u3002<\/p>\n<p><strong>\u4f18\u5316\u65b9\u6848<\/strong><\/p>\n<p><strong>\u65b9\u6848\u4e00\uff1a\u4f7f\u7528 switch-case \u8bed\u53e5<\/strong><\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5c06\u4e0d\u540c\u7684\u5224\u522b\u6761\u4ef6\u6620\u5c04\u5230\u7279\u5b9a\u7684\u6574\u578b\u503c\uff0c\u7136\u540e\u4f7f\u7528 switch-case \u8bed\u53e5\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n<pre>public class judgeoptimize {\n\n    private static final int code_condition_one = 0000;\n    private static final int code_condition_two = 0001;\n    private static final int code_condition_three = 0011;\n    private static final int code_condition_four = 0111;\n    private static final int code_condition_five = 1111;\n\n    private static void test(string a, string b, string c, string d) {\n        switch (init(a, b, c, d)) {\n            case code_condition_one:\n                \/\/ do something ...\n                break;\n            case code_condition_two:\n                \/\/ do something ...\n                break;\n            case code_condition_three:\n                \/\/ do something ...\n                break;\n            case code_condition_four:\n                \/\/ do something ...\n                break;\n            case code_condition_five:\n                \/\/ do something ...\n                break;\n        }\n    }\n\n    private static int init(string... allparam) {\n        if (allparam == null) {\n            return 1;\n        }\n        string resultnumberstr = \"\";\n        for (string s : allparam) {\n            resultnumberstr += stringutils.isnotempty(s) ? 0 : 1;\n        }\n        return integer.valueof(resultnumberstr);\n    }\n}<\/pre>\n<p><strong>\u65b9\u6848\u4e8c\uff1a\u4f7f\u7528\u81ea\u52a8\u5316\u7684\u53cd\u5c04<\/strong><\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u53cd\u5c04\u6765\u52a8\u6001\u8c03\u7528\u9884\u5b9a\u4e49\u7684\u5904\u7406\u65b9\u6cd5\u3002\u5f53\u5224\u522b\u6761\u4ef6\u6570\u91cf\u8f83\u591a\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u66f4\u7075\u6d3b\u548c\u53ef\u7ef4\u62a4\u3002<\/p>\n<pre>import java.lang.reflect.InvocationTargetException;\nimport java.lang.reflect.Method;\nimport java.util.HashMap;\nimport java.util.List;\nimport java.util.Arrays;\n\npublic class SolutionTwo {\n\n    private static Object result;\n\n    private static class Action {\n        private Class&lt;?&gt; targetInvokeClass;\n        private String targetMethodName;\n        private Class&lt;?&gt;[] parameterTypes;\n        private Object[] params;\n        private boolean isCurrentClass;\n        private boolean isNoParams;\n\n        public Action() {\n        }\n\n        public Action(Class&lt;?&gt; targetInvokeClass, String targetMethodName, Class&lt;?&gt;[] parameterTypes, Object[] params, boolean isCurrentClass, boolean isNoParams) {\n            this.targetInvokeClass = targetInvokeClass;\n            this.targetMethodName = targetMethodName;\n            this.parameterTypes = parameterTypes;\n            this.params = params;\n            this.isCurrentClass = isCurrentClass;\n            this.isNoParams = isNoParams;\n        }\n    }\n\n    private static final HashMap&lt;Object, Action&gt; ACTION_MAP = new HashMap&lt;&gt;();\n\n    static class ResultAction {\n        public void helloWorld(String name) {\n            System.out.println(name + \",\u4f60\u597d\u4e16\u754c\");\n        }\n    }\n\n    static {\n        \/\/ \u90fd\u4e0d\u4e3a\u7a7a\n        ACTION_MAP.put(0, new Action(ResultAction.class, \"helloWorld\", new Class[]{String.class}, new String[]{\"\u6211\u662f\u5f20\u4e09\"}, false, false));\n        \/\/ abc\u4e0d\u4e3a\u7a7a\uff0cd\u4e3a\u7a7a\n        ACTION_MAP.put(1, new Action(ResultAction.class, \"helloWorld\", new Class[]{String.class}, new String[]{\"\u6211\u662f\u674e\u56db\"}, false, false));\n        \/\/ ab\u4e0d\u4e3a\u7a7a\uff0ccd\u4e3a\u7a7a\n        ACTION_MAP.put(11, new Action(ResultAction.class, \"helloWorld\", new Class[]{String.class}, new String[]{\"\u6211\u662f\u738b\u4e94\"}, false, false));\n        \/\/ a\u4e0d\u4e3a\u7a7a\uff0cbcd\u4e3a\u7a7a\n        ACTION_MAP.put(111, new Action(ResultAction.class, \"helloWorld\", new Class[]{String.class}, new String[]{\"\u6211\u662f\u8d75\u516d\"}, false, false));\n        \/\/ abcd\u90fd\u4e3a\u7a7a\n        ACTION_MAP.put(1111, new Action(ResultAction.class, \"helloWorld\", new Class[]{String.class}, new String[]{\"\u6211\u662f\u5f20\u4e09\u4ed6\u7239\"}, false, false));\n    }\n\n    private static void test(String a, String b, String c, String d) {\n        int result = init(a, b, c, d);\n        Action action = ACTION_MAP.get(result);\n        if (action != null) {\n            handler(action);\n        } else {\n            System.out.println(\"\u672a\u627e\u5230\u5bf9\u5e94\u6620\u5c04\u952e\");\n        }\n    }\n\n    private static void handler(Action action) {\n        try {\n            Method method = null;\n            Class&lt;?&gt; cls = null;\n            if (action.isCurrentClass()) {\n                method = getMethod(SolutionTwo.class, action.getTargetMethodName(), action.getParameterTypes());\n                cls = SolutionTwo.class;\n            } else {\n                Class&lt;?&gt; targetClass = action.getTargetInvokeClass();\n                method = getMethod(targetClass, action.getTargetMethodName(), action.getParameterTypes());\n                cls = action.getTargetInvokeClass();\n            }\n            result = method.invoke(cls.newInstance(), action.getParams());\n        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {\n            e.printStackTrace();\n        }\n    }\n\n    private static Method getMethod(Class&lt;?&gt; cls, String methodName, Class&lt;?&gt;[] parameterTypes) throws NoSuchMethodException {\n        return cls.getMethod(methodName, parameterTypes);\n    }\n\n    private static int init(String... allParam) {\n        if (allParam == null) {\n            return 1;\n        }\n        String resultNumberStr = \"\";\n        for (String s : allParam) {\n            resultNumberStr += StringUtils.isNotEmpty(s) ? 0 : 1;\n        }\n        return Integer.valueOf(resultNumberStr);\n    }\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aJava \u4e2d\u5982\u4f55\u4f18\u5316\u9012\u8fdb\u5f0f\u5224\u65ad\uff1f\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>Java \u4e2d\u5982\u4f55\u4f18\u5316\u9012\u8fdb\u5f0f\u5224\u65ad\uff1f&#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-203500","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203500","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=203500"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203500\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}