class_inheritance_function_call.carbon 916 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. // AUTOUPDATE
  6. // RUN: %{explorer-run}
  7. // RUN: %{explorer-run-trace}
  8. // CHECK:STDOUT: Class C, call 0
  9. // CHECK:STDOUT: Class D, call 0
  10. // CHECK:STDOUT: Class C, call 1
  11. // CHECK:STDOUT: Class C, call 2
  12. // CHECK:STDOUT: Class D, call 2
  13. // CHECK:STDOUT: result: 0
  14. package ExplorerTest api;
  15. base class C {
  16. fn FunctionC(var i: i32) {
  17. Print("Class C, call {0}", i);
  18. }
  19. }
  20. class D extends C {
  21. fn FunctionD(var i: i32) {
  22. C.FunctionC(i);
  23. Print("Class D, call {0}", i);
  24. }
  25. }
  26. fn Main() -> i32 {
  27. // Calling function from class type
  28. D.FunctionD(0);
  29. // Note: D.FunctionC(0); is not accessible
  30. // Calling function from class object
  31. var d: D = {.base={}};
  32. d.FunctionC(1);
  33. d.FunctionD(2);
  34. return 0;
  35. }