Created
August 17, 2023 13:05
-
-
Save SiAust/18c923b5d82d1545fbe99776852306b6 to your computer and use it in GitHub Desktop.
Enum abrastract method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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