mult_inheritance.go 656 B

1234567891011121314151617181920212223242526272829303132333435
  1. // mult_inheritance.go
  2. package main
  3. import "fmt"
  4. type Camera struct{}
  5. func (c *Camera) TakeAPicture() string {
  6. return "Click"
  7. }
  8. type Phone struct{}
  9. func (p *Phone) Call() string {
  10. return "Ring Ring"
  11. }
  12. // multiple inheritance
  13. type CameraPhone struct {
  14. Camera
  15. Phone
  16. }
  17. func main() {
  18. cp := new(CameraPhone)
  19. fmt.Println("Our new CameraPhone exhibits multiple behaviors ...")
  20. fmt.Println("It exhibits behavior of a Camera: ", cp.TakeAPicture())
  21. fmt.Println("It works like a Phone too: ", cp.Call())
  22. }
  23. /* Output:
  24. Our new CameraPhone exhibits multiple behaviors ...
  25. It exhibits behavior of a Camera: Click
  26. It works like a Phone too: Ring Ring
  27. */