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