init_optionals.gravity 520 B

1234567891011121314151617181920212223242526272829
  1. #unittest {
  2. name: "Optional arguments in constructor";
  3. result: 22200;
  4. };
  5. class Foo {
  6. var v1;
  7. var v2;
  8. func init (p1, p2) {
  9. if (p1) v1 = p1;
  10. else v1 = 100;
  11. if (p2) v2 = p2;
  12. else v2 = 200
  13. }
  14. func tot () {
  15. return v1 * v2;
  16. }
  17. }
  18. func main() {
  19. var f1 = Foo(); // 100 * 200 = 20000
  20. var f2 = Foo(10); // 10 * 200 = 2000
  21. var f3 = Foo(10, 20); // 10 * 20 = 200
  22. return f1.tot() + f2.tot() + f3.tot();
  23. }