{"id":35975,"date":"2024-11-26T08:58:49","date_gmt":"2024-11-26T00:58:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35975\/"},"modified":"2024-11-26T08:58:49","modified_gmt":"2024-11-26T00:58:49","slug":"java-%e4%b8%ad%e9%80%92%e8%bf%9b%e5%bc%8f%e5%88%a4%e6%96%ad%e7%9a%84%e4%bc%98%e5%8c%96%e6%96%b9%e6%a1%88%ef%bc%9a%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d%e9%87%8d%e5%a4%8d%e7%9a%84-if-else-%e8%af%ad","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35975\/","title":{"rendered":"Java \u4e2d\u9012\u8fdb\u5f0f\u5224\u65ad\u7684\u4f18\u5316\u65b9\u6848\uff1a\u5982\u4f55\u907f\u514d\u91cd\u590d\u7684 if-else \u8bed\u53e5\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173085932848752.jpg\" class=\"aligncenter\" title=\"Java \u4e2d\u9012\u8fdb\u5f0f\u5224\u65ad\u7684\u4f18\u5316\u65b9\u6848\uff1a\u5982\u4f55\u907f\u514d\u91cd\u590d\u7684 if-else \u8bed\u53e5\uff1f\u63d2\u56fe\" alt=\"Java \u4e2d\u9012\u8fdb\u5f0f\u5224\u65ad\u7684\u4f18\u5316\u65b9\u6848\uff1a\u5982\u4f55\u907f\u514d\u91cd\u590d\u7684 if-else \u8bed\u53e5\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>java\u4e2d\u9012\u8fdb\u5f0f\u5224\u65ad\u7684\u4f18\u5316\u65b9\u6848<\/strong><\/p>\n<p>\u5728java\u4e2d\u8fdb\u884c\u9012\u8fdb\u5f0f\u5224\u65ad\u65f6\uff0c\u9700\u8981\u6839\u636e\u6bcf\u4e00\u5c42\u7684\u6761\u4ef6\u662f\u5426\u6ee1\u8db3\u6765\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u903b\u8f91\u3002\u539f\u59cb\u4ee3\u7801\u4e2d\u4f7f\u7528\u5927\u91cf\u7684if-else\u8bed\u53e5\uff0c\u4ee3\u7801\u5197\u4f59\u4e14\u53ef\u8bfb\u6027\u5dee\u3002\u4e0b\u9762\u4ecb\u7ecd\u4e24\u79cd\u4f18\u5316\u65b9\u6848\uff1a<\/p>\n<p><strong>\u65b9\u6848\u4e00\uff1a\u4f7f\u7528-case<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5148\u6839\u636e\u4e0d\u540c\u7684\u60c5\u51b5\u7ec4\u5408\uff0c\u751f\u6210\u4e00\u4e2a\u5bf9\u5e94\u7684\u72b6\u6001\u7801\u3002\u7136\u540e\u901a\u8fc7switch-case\u8bed\u53e5\u6839\u636e\u72b6\u6001\u7801\u76f4\u63a5\u6267\u884c\u76f8\u5e94\u7684\u4ee3\u7801\u903b\u8f91\u3002\u8be5\u65b9\u6848\u53ef\u4ee5\u51cf\u5c11if-else\u8bed\u53e5\u7684\u5c42\u7ea7\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>private static final int code_condition_one = 0000;\nprivate static final int code_condition_two = 0001;\nprivate static final int code_condition_three = 0011;\nprivate static final int code_condition_four = 0111;\nprivate static final int code_condition_five = 1111;\n\nprivate 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\nprivate 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}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6848\u4e8c\uff1a\u4f7f\u7528\u53cd\u5c04<\/strong><\/p>\n<p>\u8be5\u65b9\u6848\u901a\u8fc7\u53cd\u5c04\u673a\u5236\uff0c\u6839\u636e\u4e0d\u540c\u7684\u72b6\u6001\u7801\u52a8\u6001\u751f\u6210\u5bf9\u5e94\u7684action\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528\u5bf9\u8c61\u7684\u7279\u5b9a\u65b9\u6cd5\u6267\u884c\u4ee3\u7801\u903b\u8f91\u3002\u8be5\u65b9\u6848\u66f4\u52a0\u7075\u6d3b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u52a8\u6001\u6dfb\u52a0\u65b0\u7684\u5224\u65ad\u6761\u4ef6\u548c\u6267\u884c\u903b\u8f91\u3002<\/p>\n<pre>static class Action{\n    private Class targetInvokeClass;\n    private String targetMethodName;\n    private Class[] parameterTypes;\n    private Object[] params;\n    private boolean isCurrentClass;\n    private boolean isNoParams;\n\n    \/\/ getters and setters\n}\n\nprivate static HashMap&lt;Object,Action&gt; ACTION_MAP = new HashMap&lt;&gt;();\n\nprivate 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\nprivate static void handler(Action action) {\n    try {\n        Method method = null;\n        Class cls = null;\n        if (action.isCurrentClass()) {\n            method = getMethod(SolutionTwo.class,action.getTargetMethodName(),action.getParameterTypes());\n            cls = SolutionTwo.class;\n        }else{\n            Class 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\nprivate static Method getMethod(Class cls,String methodName,Class[] parameterTypes) throws NoSuchMethodException {\n    return cls.getMethod(methodName,parameterTypes);\n}\n\n\nprivate 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}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJava \u4e2d\u9012\u8fdb\u5f0f\u5224\u65ad\u7684\u4f18\u5316\u65b9\u6848\uff1a\u5982\u4f55\u907f\u514d\u91cd\u590d\u7684 if-else \u8bed\u53e5\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\u7684\u4f18\u5316\u65b9\u6848 \u5728java\u4e2d\u8fdb\u884c\u9012\u8fdb\u5f0f\u5224\u65ad\u65f6\uff0c\u9700\u8981\u6839\u636e\u6bcf\u4e00\u5c42\u7684\u6761\u4ef6\u662f\u5426\u6ee1\u8db3\u6765\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u903b\u8f91\u3002\u539f\u59cb\u4ee3\u7801\u4e2d\u4f7f\u7528\u5927\u91cf\u7684if-else\u8bed\u53e5\uff0c\u4ee3\u7801\u5197\u4f59\u4e14\u53ef\u8bfb\u6027\u5dee\u3002\u4e0b\u9762\u4ecb\u7ecd\u4e24\u79cd\u4f18\u5316\u65b9\u6848\uff1a \u65b9\u6848\u4e00\uff1a\u4f7f\u7528-case \u6211\u4eec\u53ef\u4ee5\u5148\u6839\u636e\u4e0d\u540c\u7684\u60c5\u51b5\u7ec4\u5408\uff0c\u751f\u6210\u4e00\u4e2a\u5bf9\u5e94\u7684\u72b6\u6001\u7801\u3002\u7136\u540e\u901a\u8fc7switch-case\u8bed\u53e5\u6839\u636e\u72b6\u6001\u7801\u76f4\u63a5\u6267\u884c\u76f8\u5e94\u7684\u4ee3\u7801\u903b\u8f91\u3002\u8be5\u65b9\u6848\u53ef\u4ee5\u51cf\u5c11if-else\u8bed\u53e5\u7684\u5c42\u7ea7\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 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 a,string b,string c,string d){ switch (init(a,b,c,d)){ case code_condition_one: \/\/ do something [&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-35975","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35975","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=35975"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35975\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}