Skip to content

Instantly share code, notes, and snippets.

@SiAust
Created August 17, 2023 13:05
Show Gist options
  • Save SiAust/18c923b5d82d1545fbe99776852306b6 to your computer and use it in GitHub Desktop.
Save SiAust/18c923b5d82d1545fbe99776852306b6 to your computer and use it in GitHub Desktop.
Enum abrastract method
class ArithmeticFunction {
private static enum Operation {
add {
@Override
int apply(int a, int b) {
return a + b;
}
}, subtract {
@Override
int apply(int a, int b) {
return a - b;
}
}, multiply {
@Override
int apply(int a, int b) {
return a * b;
}
}, divide {
@Override
int apply(int a, int b) {
return a / b;
}
};
abstract int apply(int a, int b);
}
public static int arithmetic(int a, int b, String operator) {
return Operation.valueOf(operator).apply(a, b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment