class_inheritance_shadow.carbon 810 B

1234567891011121314151617181920212223242526272829303132333435363738
  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 extends C {
  21. fn Method1() {
  22. Print("Class D");
  23. }
  24. var value: i32;
  25. }
  26. fn Main() -> i32 {
  27. // Initialize derived value first, base value second
  28. var d: D = {.value = 2, .base={.value = 1}};
  29. d.Method1();
  30. Print("d.value = {0}", d.value);
  31. Print("d.GetValue() = {0}", d.GetValue());
  32. return 0;
  33. }