class_inheritance_function_call.carbon 873 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // NOAUTOUPDATE
  6. package ExplorerTest api;
  7. base class C {
  8. fn FunctionC(var i: i32) {
  9. Print("Class C, call {0}", i);
  10. }
  11. }
  12. class D {
  13. extend base: C;
  14. fn FunctionD(var i: i32) {
  15. C.FunctionC(i);
  16. Print("Class D, call {0}", i);
  17. }
  18. }
  19. fn Main() -> i32 {
  20. // Calling function from class type
  21. D.FunctionD(0);
  22. // Note: D.FunctionC(0); is not accessible
  23. // Calling function from class object
  24. var d: D = {.base={}};
  25. d.FunctionC(1);
  26. d.FunctionD(2);
  27. return 0;
  28. }
  29. // CHECK:STDOUT: Class C, call 0
  30. // CHECK:STDOUT: Class D, call 0
  31. // CHECK:STDOUT: Class C, call 1
  32. // CHECK:STDOUT: Class C, call 2
  33. // CHECK:STDOUT: Class D, call 2
  34. // CHECK:STDOUT: result: 0