猿问

替换 switch 语句 Java

我一直想知道是否有办法替换我拥有的当前 switch 语句。下面是我拥有的代码示例,尽管我拥有的语句要长得多,而且只会变得更大。switch 方法通过文件读取器调用,因此它读取一行,然后调用此函数并分配值。


public static void example(String action, String from, String to){

 switch (action) {

           case ("run"):

                runTo(from,to);

                break;

           case ("walk"):

                walkTo(from,to);

                break;

           case ("hide"):

                hideAt(to);

                break;

            }

 }

编辑:我很好奇是否有更好的方法来代替使用上述场景的 switch 语句。


我对示例进行了一些更新,以使其更有意义。一些方法调用不需要使用所有参数。


郎朗坤
浏览 288回答 4
4回答

幕布斯7119047

对于 Java 7 及以下版本,我们可以为函数实现声明一个接口。对于 Java 8+,我们可以使用Function接口。界面:public interface FunctionExecutor {&nbsp; &nbsp; public Object execute(String from,String to);}函数上下文:public class FunctionContect {&nbsp; &nbsp;HashMap<String, FunctionExecutor> context=new HashMap<String, FunctionExecutor>();&nbsp; &nbsp; public void register(String name,FunctionExecutor function){&nbsp; &nbsp; &nbsp; &nbsp; context.put(name, function);&nbsp; &nbsp; }&nbsp; &nbsp;public Object call(String name,String from,String to){&nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; context.get(name).execute(from, to);&nbsp; &nbsp;}&nbsp; &nbsp;public FunctionExecutor get(String name){&nbsp; &nbsp; &nbsp; return context.get(name);&nbsp; &nbsp;}&nbsp; }功能实现:public class RunFunctionImpl implements FunctionExecutor{&nbsp; &nbsp; @Override&nbsp; &nbsp; public Object execute(String from, String to) {&nbsp; &nbsp; &nbsp; &nbsp;System.out.println("function run");&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp;}}// OTHER FUCNTIONS注册功能:&nbsp; &nbsp; FunctionContect contex = new FunctionContect();&nbsp; &nbsp; contex.register("run", new RunFunctionImpl());&nbsp; &nbsp; contex.register("walk", new WalkFunctionImpl());&nbsp; &nbsp; contex.register("hide", new HideFunctionImpl());调用函数&nbsp;context.call(action, from, to);或者&nbsp;context.get(action).execute(from,to);

倚天杖

摆脱 switch 的一种可能选择是使用函数的 hashmap:private String stringMethod(final String action, final String source) {&nbsp; &nbsp; final Function<String, String> toLowerFunction = String::toLowerCase;&nbsp; &nbsp; final Function<String, String> toUpperFunction = String::toUpperCase;&nbsp; &nbsp; final HashMap<String, Function<String, String>> stringFunctions = new HashMap<>();&nbsp; &nbsp; stringFunctions.put("toLower", toLowerFunction);&nbsp; &nbsp; stringFunctions.put("toUpper", toUpperFunction);&nbsp; &nbsp; return stringFunctions.get(action).apply(source);}

海绵宝宝撒

我不完全确定你想要实现什么。如果你不想继续添加新的case ("ccc"):&nbsp; Lmn(b,c,i);&nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;块。您可以散列 a 中的HashMap<string, method>方法并使用键从映射中获取方法并执行它。

冉冉说

如果您在同一个变量上有重复的 switch 案例,例如在 methodf和. 然后你可以把事情从里到外:ghvoid f(String a) {&nbsp; &nbsp; switch (a) {&nbsp; &nbsp; case "aaa": ... ; break;&nbsp; &nbsp; ...&nbsp; &nbsp; }}void g(String a) {&nbsp; &nbsp; switch (a) {&nbsp; &nbsp; case "aaa": ... ; break;&nbsp; &nbsp; case "bbb": ... ; break;&nbsp; &nbsp; case "ccc": ... ; break;&nbsp; &nbsp; ...&nbsp; &nbsp; }}void h(String a) {&nbsp; &nbsp; switch (a) {&nbsp; &nbsp; case "aaa": ... ; break;&nbsp; &nbsp; ...&nbsp; &nbsp; }}可以将面向对象处理为:class C {&nbsp; &nbsp; public f() { }&nbsp; &nbsp; public g() { }&nbsp; &nbsp; public h() { }}class Aaa extends C {&nbsp; &nbsp; @Override&nbsp; &nbsp; public f() { test3(b,c); } // Or even just the body of test3&nbsp; &nbsp; @Override&nbsp; &nbsp; public g() { }&nbsp; &nbsp; @Override&nbsp; &nbsp; public h() { }}class Bbb extends C {}class Ccc extends C {}然后一旦必须提供特定的 C:&nbsp; &nbsp; C c;&nbsp; &nbsp; switch (a) {&nbsp; &nbsp; case "aaa": c = new Aaa(); break;&nbsp; &nbsp; case "bbb": c = new Bbb(); break;&nbsp; &nbsp; case "ccc": c = new Ccc(); break;&nbsp; &nbsp; ...&nbsp; &nbsp; }&nbsp; &nbsp; c.f(...);&nbsp; &nbsp; c.g(...);&nbsp; &nbsp; c.h(...);这看起来是间接的,但实际上提高了开发质量。添加新案例并不意味着搜索所有开关案例。一种情况(“aaa”)的代码都在一个类中,具有自己的专用字段。这可以简化事情并提供更好的概览。
随时随地看视频慕课网APP

相关分类

Java
我要回答