inherit_methods.go 569 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package main
  2. import "fmt"
  3. type Base struct {
  4. id string
  5. }
  6. func (b *Base) Id() string {
  7. return b.id
  8. }
  9. func (b *Base) SetId(id string) {
  10. b.id = id
  11. }
  12. type Person struct {
  13. Base
  14. FirstName string
  15. LastName string
  16. }
  17. type Employee struct {
  18. Person
  19. salary float32
  20. }
  21. func main() {
  22. idjb := Base{"007"}
  23. jb := Person{idjb, "James", "Bond"}
  24. e := &Employee{jb, 100000.}
  25. fmt.Printf("ID of our hero: %v\n", e.Id())
  26. // Change the id:
  27. e.SetId("007B")
  28. fmt.Printf("The new ID of our hero: %v\n", e.Id())
  29. }
  30. /* Output:
  31. ID of our hero: 007
  32. The new ID of our hero: 007B
  33. */