根据整数值使用不同的方法
Use different methods based on an integer value
这是我无法理解的事情,现在我有这样的事情:
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);
}
}
我想要的是将开关从循环中取出,因为 someInt 对每个 i 都保持不变,因此只需要决定一次,但我需要为每个 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
}
您可以用多态替换您的条件:
一些例子:
- http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html
- https://sourcemaking.com/refactoring/replace-conditional-with-polymorphism
- http://tmont.com/blargh/2011/11/refactoring-a-switch-statement
您的代码示例:
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);
}
}
您可以使用 Java 8 个方法参考。
- https://blog.idrsolutions.com/2015/02/java-8-method-references-explained-5-minutes/
- https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
这是一个例子:
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);
}
}
}
我的两分钱。如果我们开始发挥作用,那就让我们坚持到底吧!
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
});
另外,我在您的 switch 中没有看到任何 break 语句。也许是因为这只是一个例子,但请检查它们是否在这里。否则,您将在所有情况下获得 method2()。
这是我无法理解的事情,现在我有这样的事情:
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);
}
}
我想要的是将开关从循环中取出,因为 someInt 对每个 i 都保持不变,因此只需要决定一次,但我需要为每个 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
}
您可以用多态替换您的条件:
一些例子:
- http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html
- https://sourcemaking.com/refactoring/replace-conditional-with-polymorphism
- http://tmont.com/blargh/2011/11/refactoring-a-switch-statement
您的代码示例:
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);
}
}
您可以使用 Java 8 个方法参考。
- https://blog.idrsolutions.com/2015/02/java-8-method-references-explained-5-minutes/
- https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
这是一个例子:
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);
}
}
}
我的两分钱。如果我们开始发挥作用,那就让我们坚持到底吧!
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
});
另外,我在您的 switch 中没有看到任何 break 语句。也许是因为这只是一个例子,但请检查它们是否在这里。否则,您将在所有情况下获得 method2()。