[关闭]
@phper 2018-03-13T10:06:20.000000Z 字数 3791 阅读 6258

27.用组合代替继承-golang中的面向对象

Golang

原文:https://golangbot.com/inheritance/


欢迎访问Golang 系列教程中的第27章。

go 不支持继承(inheritance), 但是它支持组合(composition)。构图的一般定义是 "放在一起"。组合的一个示例是小轿车。汽车由轮子、引擎和其他各种各样的零件组成。

通过嵌入结构进行组合

通过将一个结构类型嵌入到另一个中, 可以在Go中实现组合。

博客文章是作文的完美典范。每个博客帖子都有标题、内容和作者信息。这可以用构图完美地表示出来。在本教程的下一个步骤中, 我们将学习如何完成这项工作。

让我们首先创建author结构。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type author struct {
  6. firstName string
  7. lastName string
  8. bio string
  9. }
  10. func (a author) fullName() string {
  11. return fmt.Sprintf("%s %s", a.firstName, a.lastName)
  12. }

在上面的代码段中, 我们创建了一个具有字段firstNamelastNamebioauthor结构。我们还添加了一个方法fullName() , 并将author作为接收器类型, 并返回作者的全名。

下一步是创建post结构。

  1. type post struct {
  2. title string
  3. content string
  4. author
  5. }
  6. func (p post) details() {
  7. fmt.Println("Title: ", p.title)
  8. fmt.Println("Content: ", p.content)
  9. fmt.Println("Author: ", p.author.fullName())
  10. fmt.Println("Bio: ", p.author.bio)
  11. }

post 结构具有字段titlecontent。它还具有嵌入的匿名字段author。此字段表示post结构由author组成。现在, post结构可以访问author结构的所有字段和方法。我们还在post结构中添加了details()方法, 它打印作者的标题、内容、fullName 和bio。

每当一个结构字段嵌入到另一个域中时, 就可以选择访问嵌入字段, 就像它们是外部结构的一部分一样。这意味着上述代码的11行中的p.author.fullName()可以替换为p.fullName()。因此,details()方法可以按如下方式重写,

  1. func (p post) details() {
  2. fmt.Println("Title: ", p.title)
  3. fmt.Println("Content: ", p.content)
  4. fmt.Println("Author: ", p.fullName())
  5. fmt.Println("Bio: ", p.bio)
  6. }

现在, 我们已经准备好了authorpost结构, 让我们通过创建一个博客文章来完成这个程序。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type author struct {
  6. firstName string
  7. lastName string
  8. bio string
  9. }
  10. func (a author) fullName() string {
  11. return fmt.Sprintf("%s %s", a.firstName, a.lastName)
  12. }
  13. type post struct {
  14. title string
  15. content string
  16. author
  17. }
  18. func (p post) details() {
  19. fmt.Println("Title: ", p.title)
  20. fmt.Println("Content: ", p.content)
  21. fmt.Println("Author: ", p.fullName())
  22. fmt.Println("Bio: ", p.bio)
  23. }
  24. func main() {
  25. author1 := author{
  26. "Naveen",
  27. "Ramanathan",
  28. "Golang Enthusiast",
  29. }
  30. post1 := post{
  31. "Inheritance in Go",
  32. "Go supports composition instead of inheritance",
  33. author1,
  34. }
  35. post1.details()
  36. }

在playground上运行

上述程序中的主要功能在31行创建了一个新的作者。通过嵌入author1, 在36行创建了一个新帖子。此程序打印,

  1. Title: Inheritance in Go
  2. Content: Go supports composition instead of inheritance
  3. Author: Naveen Ramanathan
  4. Bio: Golang Enthusiast

结构的嵌入切片

我们可以进一步采取此示例, 并使用博客post的切片创建网站。

让我们先定义website结构。请将以下代码添加到现有程序的主要功能之上并运行它。

  1. type website struct {
  2. []post
  3. }
  4. func (w website) contents() {
  5. fmt.Println("Contents of Website\n")
  6. for _, v := range w.posts {
  7. v.details()
  8. fmt.Println()
  9. }
  10. }

在添加上面的代码后运行上面的程序时, 编译器将抱怨以下错误,

main.go:31:9: syntax error: unexpected [, expecting field name or embedded type  

此错误指向结构[]post的嵌入切片。原因是不可能匿名嵌入切片。需要字段名。因此, 让我们修复这个错误, 使编译器正常运行。

  1. type website struct {
  2. posts []post
  3. }

我已将字段名 post 添加到 []post切片posts中。

现在, 让我们修改主要功能, 并为我们的新网站创建一些帖子。

修改主功能后的完整程序如下所示,

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type author struct {
  6. firstName string
  7. lastName string
  8. bio string
  9. }
  10. func (a author) fullName() string {
  11. return fmt.Sprintf("%s %s", a.firstName, a.lastName)
  12. }
  13. type post struct {
  14. title string
  15. content string
  16. author
  17. }
  18. func (p post) details() {
  19. fmt.Println("Title: ", p.title)
  20. fmt.Println("Content: ", p.content)
  21. fmt.Println("Author: ", p.fullName())
  22. fmt.Println("Bio: ", p.bio)
  23. }
  24. type website struct {
  25. posts []post
  26. }
  27. func (w website) contents() {
  28. fmt.Println("Contents of Website\n")
  29. for _, v := range w.posts {
  30. v.details()
  31. fmt.Println()
  32. }
  33. }
  34. func main() {
  35. author1 := author{
  36. "Naveen",
  37. "Ramanathan",
  38. "Golang Enthusiast",
  39. }
  40. post1 := post{
  41. "Inheritance in Go",
  42. "Go supports composition instead of inheritance",
  43. author1,
  44. }
  45. post2 := post{
  46. "Struct instead of Classes in Go",
  47. "Go does not support classes but methods can be added to structs",
  48. author1,
  49. }
  50. post3 := post{
  51. "Concurrency",
  52. "Go is a concurrent language and not a parallel one",
  53. author1,
  54. }
  55. w := website{
  56. posts: []post{post1, post2, post3},
  57. }
  58. w.contents()
  59. }

在playground上运行

在上面的主要功能中, 我们创建了一个作者author1和三个帖子post1、 post2和post3。最后, 通过嵌入3个帖子并在下一行中显示内容, 我们创建了62行中的网站w 。

这个程序将输出,

  1. Contents of Website
  2. Title: Inheritance in Go
  3. Content: Go supports composition instead of inheritance
  4. Author: Naveen Ramanathan
  5. Bio: Golang Enthusiast
  6. Title: Struct instead of Classes in Go
  7. Content: Go does not support classes but methods can be added to structs
  8. Author: Naveen Ramanathan
  9. Bio: Golang Enthusiast
  10. Title: Concurrency
  11. Content: Go is a concurrent language and not a parallel one
  12. Author: Naveen Ramanathan
  13. Bio: Golang Enthusiast

这使我们到本教程的末尾。有一个伟大的一天。请留下您的反馈和意见。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注