[译] part 27: golang 的面向对象 — 组合取代继承

https://juejin.im/post/5caf7112f265da03981fb501

Go 不支持继承,但它支持组合。组合的通用定义是“放在一起”。组合的一个例子是汽车。汽车由车轮,发动机和各种其他部件组成。

通过嵌入结构组合

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. }

在上面的代码片段中,我们创建了一个包含firstNamelastNamebio字段的author结构。我们还添加了一个方法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()方法,用于打印作者的titlecontentfullNamebio

每当一个结构嵌入另一个结构时,Go 为我们提供了访问嵌入字段的选项,就好像它们是外部结构的一部分一样。这意味着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. }

Run in playground

上面程序中的主要功能是在第 31 行中创建一个新的作者,在第 36 行嵌入该作者然后创建一个新帖子。这个程序打印,

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

嵌入结构切片

我们可以进一步采用这个例子,并使用一些博客帖子创建一个网站:)。

让我们先定义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. }

在添加上面的代码后,运行时编译器会报以下错误,

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

此错误指向结构[]post的嵌入切片。原因是不能匿名嵌入一个切片。字段名称是必需的。所以让我们修复这个错误。

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

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

现在让我们修改main function并为我们的新网站创建一些帖子。

修改后的完整程序如下,

  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. }

Run in playground

在上面的main function中,我们创建了一个作者author1和三个帖子post1post2post3。最后我们在第 62 行创建了网站w。 通过嵌入这 3 个帖子并在下一行显示内容。

程序输出,

  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
ft_authoradmin  ft_create_time2019-08-03 16:39
 ft_update_time2019-08-03 16:39