{"id":35910,"date":"2024-11-26T09:31:51","date_gmt":"2024-11-26T01:31:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35910\/"},"modified":"2024-11-26T09:31:51","modified_gmt":"2024-11-26T01:31:51","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:\/\/fwq.ai\/blog\/35910\/","title":{"rendered":"Java \u4e2d\u5982\u4f55\u4f18\u5316\u9012\u8fdb\u5f0f\u5224\u65ad\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173080181265854.jpg\" class=\"aligncenter\" title=\"Java \u4e2d\u5982\u4f55\u4f18\u5316\u9012\u8fdb\u5f0f\u5224\u65ad\uff1f\u63d2\u56fe\" alt=\"Java \u4e2d\u5982\u4f55\u4f18\u5316\u9012\u8fdb\u5f0f\u5224\u65ad\uff1f\u63d2\u56fe\" \/><\/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 -case \u8bed\u53e5<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJava \u4e2d\u5982\u4f55\u4f18\u5316\u9012\u8fdb\u5f0f\u5224\u65ad\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java\u4e2d\u9012\u8fdb\u5f0f\u5224\u65ad\u5982\u4f55\u4f18\u5316\uff1f \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 \u4f18\u5316\u65b9\u6848 \u65b9\u6848\u4e00\uff1a\u4f7f\u7528 -case \u8bed\u53e5 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \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 public class judgeoptimize { private static final int code_condition_one = 0000; private static final int code_condition_two = 0001; private static final int code_condition_three = 0011; private static final int code_condition_four = 0111; private static final int code_condition_five = 1111; private static void test(string [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-35910","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=35910"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35910\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}