Main.hx 796 B

1234567891011121314151617181920212223242526272829303132333435
  1. @:functionalInterface
  2. interface MathOperation {
  3. function perform(a:Int, b:Int):Int;
  4. }
  5. class Ops {
  6. static public final add:MathOperation = (a, b) -> a + b;
  7. static public final subtract:MathOperation = (a, b) -> a - b;
  8. static public function performMathOperation(operation:MathOperation) {
  9. return operation.perform(8, 4);
  10. }
  11. }
  12. class Main {
  13. static function main() {
  14. var result = Ops.performMathOperation(Ops.add);
  15. trace('Add: ${result}');
  16. result = Ops.performMathOperation(Ops.subtract);
  17. trace('Subtract: ${result}');
  18. result = Ops.performMathOperation(multiply);
  19. trace('Multiply: ${result}');
  20. result = Ops.performMathOperation(function(a, b):Int {
  21. return Std.int(a / b);
  22. });
  23. trace('Divide: ${result}');
  24. }
  25. static function multiply(a, b):Int {
  26. return a * b;
  27. }
  28. }