在java中调用基于字符串名称的函数(Invoking a functions based on string name in java)

编程入门 行业动态 更新时间:2024-10-27 18:28:07
在java中调用基于字符串名称的函数(Invoking a functions based on string name in java)

我需要一个逻辑来替换下面的代码。

void invokeMethod(String action){ if ("echo".equals(action)) { //call echo echo(); } else if ("dump".equals(action)) { // call dump dump(); } ... goes on }

切换大小写字符串参数在java 1.6中不起作用。 我可以做得更好吗?

I need a logic to replace the following code.

void invokeMethod(String action){ if ("echo".equals(action)) { //call echo echo(); } else if ("dump".equals(action)) { // call dump dump(); } ... goes on }

switch case with string parameter won't work in java 1.6. Can I do it a better way ?

最满意答案

我使用了一个带有动作的java hashmap作为键值和随机整数值。 无论何时需要调用perticular动作,从hashmap获取整数并使用switch case(在上面的问题中,字符串比较非常操作,用整数代替)。

I used a java hashmap with action as key and random integer as value. Whenever perticular action is asked to invoke, fetch the integer from the hashmap and use switch case now (In above problem, string comparison was very operation, replaced the same with integers).

更多推荐

本文发布于:2023-08-02 20:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1380617.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   函数   名称   java   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!