{"id":203616,"date":"2025-05-22T11:33:45","date_gmt":"2025-05-22T03:33:45","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203616\/"},"modified":"2025-05-22T11:33:45","modified_gmt":"2025-05-22T03:33:45","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-java-%e4%b8%ad%e4%bc%98%e5%8c%96%e5%a4%9a%e4%b8%aa%e6%9d%a1%e4%bb%b6%e7%9a%84%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\/203616\/","title":{"rendered":"\u5982\u4f55\u5728 Java \u4e2d\u4f18\u5316\u591a\u4e2a\u6761\u4ef6\u7684\u9012\u8fdb\u5f0f\u5224\u65ad\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5728 Java \u4e2d\u4f18\u5316\u591a\u4e2a\u6761\u4ef6\u7684\u9012\u8fdb\u5f0f\u5224\u65ad\uff1f<\/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\u5982\u4f55\u5728 Java \u4e2d\u4f18\u5316\u591a\u4e2a\u6761\u4ef6\u7684\u9012\u8fdb\u5f0f\u5224\u65ad\uff1f\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><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241114\/1731547855673552cf74803.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5728 java \u4e2d\uff0c\u5f53\u9700\u8981\u5bf9\u591a\u4e2a\u6761\u4ef6\u8fdb\u884c\u9012\u8fdb\u5f0f\u5224\u65ad\u65f6\uff0c\u4f20\u7edf\u7684\u5199\u6cd5\u5f80\u5f80\u6bd4\u8f83\u5197\u957f\uff0c\u8fd9\u4f1a\u5bf9\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u9020\u6210\u4e00\u5b9a\u5f71\u54cd\u3002\u4e3a\u4e86\u4f18\u5316\u8fd9\u7c7b\u4ee3\u7801\uff0c\u6709\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6848\uff1a<\/p>\n<p><strong>\u65b9\u6848\u4e00\uff1a\u4f7f\u7528\u679a\u4e3e\u548cswitch \u8bed\u53e5<\/strong><\/p>\n<p>\u901a\u8fc7\u5c06\u4e0d\u540c\u6761\u4ef6\u7ec4\u5408\u6210\u4e00\u4e2a\u679a\u4e3e\u7c7b\uff0c\u5e76\u5229\u7528switch \u8bed\u53e5\u8fdb\u884c\u5224\u65ad\uff0c\u53ef\u4ee5\u5927\u5e45\u7b80\u5316\u4ee3\u7801\u3002 <\/p>\n<pre>public class conditionenum {\n\n    public static final int condition_one = 0000; \/\/ \u90fd\u4e0d\u4e3a\u7a7a\n    public static final int condition_two = 0001; \/\/ abc \u4e0d\u4e3a\u7a7a\u4e14 d \u4e3a\u7a7a\n    public static final int condition_three = 0011; \/\/ ab \u4e0d\u4e3a\u7a7a\u4e14 cd \u4e3a\u7a7a\n    public static final int condition_four = 0111; \/\/ a \u4e0d\u4e3a\u7a7a\u4e14 bcd \u4e3a\u7a7a\n    public static final int condition_five = 1111; \/\/ \u90fd\u4e3a\u7a7a\n\n}\n\npublic class optimizedemo {\n\n    public static void test(string a, string b, string c, string d) {\n        int condition = init(a, b, c, d);\n        switch (condition) {\n            case conditionenum.condition_one:\n                \/\/ \u90fd\u4e0d\u4e3a\u7a7a\n                break;\n            case conditionenum.condition_two:\n                \/\/ abc \u4e0d\u4e3a\u7a7a\u4e14 d \u4e3a\u7a7a\n                break;\n            case conditionenum.condition_three:\n                \/\/ ab \u4e0d\u4e3a\u7a7a\u4e14 cd \u4e3a\u7a7a\n                break;\n            case conditionenum.condition_four:\n                \/\/ a \u4e0d\u4e3a\u7a7a\u4e14 bcd \u4e3a\u7a7a\n                break;\n            case conditionenum.condition_five:\n                \/\/ \u90fd\u4e3a\u7a7a\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\n}<\/pre>\n<p><strong>\u65b9\u6848\u4e8c\uff1a\u4f7f\u7528\u53cd\u5c04\u5b9e\u73b0\u52a8\u6001\u8c03\u7528<\/strong><\/p>\n<p>\u901a\u8fc7\u53cd\u5c04\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u6761\u4ef6\uff0c\u52a8\u6001\u8c03\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u907f\u514d\u4f7f\u7528\u5197\u957f\u7684if-else\u8bed\u53e5\u3002 <\/p>\n<pre>import java.lang.reflect.Method;\n\npublic class ReflectOptimizeDemo {\n\n    private static Method[] methods;\n\n    static {\n        try {\n            methods = ReflectOptimizeDemo.class.getDeclaredMethods();\n        } catch (NoSuchMethodException e) {\n            e.printStackTrace();\n        }\n    }\n\n    public static void test(String a, String b, String c, String d) {\n        String condition = init(a, b, c, d);\n        for (Method method : methods) {\n            if (method.getName().equals(condition)) {\n                try {\n                    method.invoke(null, a, b, c, d);\n                } catch (IllegalAccessException | InvocationTargetException e) {\n                    e.printStackTrace();\n                }\n            }\n        }\n    }\n\n    private static String init(String... allParam) {\n        if (allParam == null) {\n            return \"allNull\";\n        }\n        String resultNumberStr = \"\";\n        for (String s : allParam) {\n            resultNumberStr += StringUtils.isNotEmpty(s) ? 0 : 1;\n        }\n        return \"condition\" + resultNumberStr;\n    }\n\n    public static void condition0000(String a, String b, String c, String d) {\n        \/\/ \u90fd\u4e0d\u4e3a\u7a7a\n    }\n\n    public static void condition0001(String a, String b, String c, String d) {\n        \/\/ abc \u4e0d\u4e3a\u7a7a\u4e14 d \u4e3a\u7a7a\n    }\n\n    public static void condition0011(String a, String b, String c, String d) {\n        \/\/ ab \u4e0d\u4e3a\u7a7a\u4e14 cd \u4e3a\u7a7a\n    }\n\n    public static void condition0111(String a, String b, String c, String d) {\n        \/\/ a \u4e0d\u4e3a\u7a7a\u4e14 bcd \u4e3a\u7a7a\n    }\n\n    public static void condition1111(String a, String b, String c, String d) {\n        \/\/ \u90fd\u4e3a\u7a7a\n    }\n\n    public static void main(String[] args) {\n        test(null, null, null, null);\n    }\n}<\/pre>\n<p>\u8fd9\u4e24\u79cd\u65b9\u6848\u90fd\u53ef\u4ee5\u663e\u7740\u63d0\u9ad8\u8fd9\u7c7b\u4ee3\u7801\u7684\u6548\u7387\u548c\u7b80\u6d01\u5ea6\uff0c\u6539\u5584\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002 <\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5728 Java \u4e2d\u4f18\u5316\u591a\u4e2a\u6761\u4ef6\u7684\u9012\u8fdb\u5f0f\u5224\u65ad\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 Java \u4e2d\u4f18\u5316\u591a\u4e2a\u6761\u4ef6&#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-203616","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203616","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=203616"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203616\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}