class_inheritance_shadow.carbon 864 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 D
  9. // CHECK:STDOUT: d.value = 2
  10. // CHECK:STDOUT: d.GetValue() = 2
  11. // CHECK:STDOUT: result: 0
  12. package ExplorerTest api;
  13. base class C {
  14. fn Method1() {
  15. Print("Class C");
  16. }
  17. fn GetValue[self: Self]() -> i32 {
  18. return self.value;
  19. }
  20. var value: i32;
  21. }
  22. class D extends C {
  23. fn Method1() {
  24. Print("Class D");
  25. }
  26. var value: i32;
  27. }
  28. fn Main() -> i32 {
  29. // Initialize derived value first, base value second
  30. var d: D = {.value = 2, .base={.value = 1}};
  31. d.Method1();
  32. Print("d.value = {0}", d.value);
  33. Print("d.GetValue() = {0}", d.GetValue());
  34. return 0;
  35. }