简体   繁体   English

根据整数值使用不同的方法

[英]Use different methods based on an integer value

this is something i cannot get my head around, now i have something like this: 这是我无法理解的东西,现在我有这样的东西:

boolean method1(int a){
//something
returns true;
}

boolean method2(int a){
//something
returns true;
}

for (int i; i<100; i++){
  switch (someInt){
  case 1: boolean x = method1(i);
  case 2: boolean x = method2(i);
  }


}

What i would like is to take the switch out of the loop, as the someInt will stay the same for every i, thus need to be decided only once, but i need x to be checked for every i, so i would need something like: 我想要的是把开关带出循环,因为someInt将保持相同的每个i,因此需要只决定一次,但我需要x检查每一个,所以我需要像:

    switch (someInt){
          case 1: method1(); //will be used in loop below
          case 2: method2(); //will be used in loop below
          }

   for (int i; i<100; i++){
       boolean x = method the switch above picked
   }

You can use Java 8 method references. 您可以使用Java 8方法引用。

Here is an example: 这是一个例子:

public class WithMethodRefs {
    interface MyReference {
        boolean method(int a);
    }

    boolean method1(int a) {
        return true;
    }

    boolean method2(int a) {
        return false;
    }

    public void doIt(int someInt) {
        MyReference p = null;
        switch (someInt) {
        case 1:
            p = this::method1;
            break;
        case 2:
            p = this::method2;
            break;
        }

        for (int i = 0; i < 100; i++) {
            p.method(i);
        }
    }
}

You can replace your conditional with polymorphism: 您可以用多态替换条件:

Some examples: 一些例子:

An example with your code: 您的代码示例:

    interface CallIt {
        boolean callMe(int a);
    }

    class Method1 implements CallIt {
        public boolean callMe(int a) {
            return true;
        }
    }

    class Method2 implements CallIt {
        public boolean callMe(int a) {
            return true;
        }
    }

    void doIt(int someInt) {
        CallIt callIt = null;
        switch (someInt) {
        case 1:
            callIt = new Method1();
            break;
        case 2:
            callIt = new Method2();
            break;
        }

        for (int i = 0; i < 100; i++) {
            boolean x = callIt.callMe(i);
        }
    }

My two cents. 我的两分钱。 If we started to be functional let's do it till the end! 如果我们开始实现功能,那就让它一直持续到最后!

    IntFunction<IntFunction<Boolean>> basic = x -> i -> {
        switch (x) {
            case 1: return method1(i);
            case 2: return method2(i);
            default:
                throw new RuntimeException("No method for " + someInt);
        }
    };
    IntFunction<Boolean> f = basic.apply(someInt);
    IntStream.range(0, 100).forEach(i -> {
        boolean result = f.apply(i);
        //do something with the result
    });

Also I don't see any break statements in your switch. 此外,我在您的交换机中看不到任何break语句。 Maybe it's because this is just an example, but check if they are here. 也许是因为这只是一个例子,但要检查它们是否在这里。 You will get method2() for all cases otherwise. 否则,您将获得所有情况的method2()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM