如何使用Switch Case在Java中实现动态方法调用

huangapple go评论72阅读模式
英文:

How to make calling a method dynamic with a 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();"

}

BigDecimal qMidRate = null;

for(MonthlyRates bar: results){
qMidRate = getoption;
//更多逻辑
}

我正在尝试从get方法中获取一个BigDecimal值,有没有办法做到这一点?任何帮助将不胜感激。
英文:

I want to call a method. Depending on a argument a certain method will be called.
Currently I am trying to achieve this using a Switch Case, but I cant get it to work.

Code:

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 
}


I am trying to get a Big Decimal value from the get methods, is there any way of doing this? any help would be appreciated.

答案1

得分: 1

如果所有这些方法都返回字符串,只需去掉双引号:

case 3:
    getoption = bar.getOneMonth();
    break;
case 4:
    getoption = bar.getTwoMonth();
    break;
case 5:
    getoption = bar.getThreeMonth();
    break;
英文:

If all those methods are returning strings, simply ditch the double quotes:

case 3:
    getoption = bar.getOneMonth();
    break;
case 4:
    getoption = bar.getTwoMonth();
    break;
case 5:
    getoption = bar.getThreeMonth();
    break;

答案2

得分: 0

你可以在使用Java 8时使用方法引用/lambda来完成这个。类似于:

    // 我不清楚'getOneMonth'等是返回什么或者有什么参数...
    // 这里假设它们接受一个Long参数,返回一个String,因为我不确定。
    Function<Long, String> choice = null;
    switch (rateIndex) {
        case 3:
            choice = bar::getOneMonth;
            break;
        case 4:
            choice = bar::getTwoMonths;
            break;
        // ...
        default:
            throw new HissyFit();
    }

    for (final MonthlyRates rates : bar.getMonthlyRates()) {
        final String methodCallResult = choice.apply(rates.getSomeLong());
    }

注意:由于你要求只返回翻译好的部分,因此我只提供了代码的翻译部分。

英文:

You can do this with method references/lambdas if you're using java 8. Something like:

    // I have no idea what &#39;getOneMonth&#39;, etc. are returning or have as parameters...
    // This assumes they take a Long, and return a String, &#39;cause I don&#39;t know.
    Function&lt;Long, String&gt; choice = null;
    switch (rateIndex) {
        case 3:
            choice = bar::getOneMonth;
            break;
        case 4:
            choice = bar::getTwoMonths;
            break;
        // ...
        default:
            throw new HissyFit();
    }

    for (final MonthlyRates rates : bar.getMonthlyRates()) {
        final String methodCallResult = choice.apply(rates.getSomeLong());
    }

huangapple
  • 本文由 发表于 2020年10月15日 00:26:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64357666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定