class_inheritance_shadow.carbon 818 B

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