java - 如何使用 Switch Case-Java 动态调用方法
问题描述
我想调用一个方法。根据参数,将调用某个方法。目前我正在尝试使用 Switch Case 来实现这一点,但我无法让它工作。
代码:
String getoption = null;
switch (rateIndex){
case 3:
getoption = "bar.getOneMonth();"
case 4:
getoption = "bar.getTwoMonth();"
case 5:
getoption = "bar.getThreeMonth();"
}
Big Decimal qMidRate = null;
for(MonthlyRates bar: results){
qMidRate = getoption;
//more logic
}
我正在尝试从 get 方法中获取 Big Decimal 值,有没有办法做到这一点?任何帮助,将不胜感激。
解决方案
如果所有这些方法都返回字符串,只需去掉双引号:
case 3:
getoption = bar.getOneMonth();
break;
case 4:
getoption = bar.getTwoMonth();
break;
case 5:
getoption = bar.getThreeMonth();
break;
推荐阅读
- python - 使用 Python 从 Instagram 读取 JSON
- c# - .NET 代码从数据访问类生成的 API 端点
- r - 结果路径不一样
- wpf - 当我在 ItemsSource 中添加新项目时,WPF ItemsControl 不更新
- amazon-web-services - 使用 AWS ALB 基于动态 id 将请求路由到特定目标
- react-native - 如何使用计时器为 useEffect 编写 JEST 测试?
- amazon-web-services - 我怎样才能得到一个image.png o jpg?在 apigateway
- elixir - 当 Elixir 模块定义了 `@callback` 并实现了指定的函数时会发生什么
- reactjs - 如何在 ReactJS 中创建和显示相同组件的多个独立实例?
- visual-studio - 更改 SVN 密码后 AnkhSVN 在 VisualStudio 2017 中挂起