go 面向对象 - 组合 替代 继承
go 不支持继承,但是,它支持组合。 组合的一般定义是“组合在一起”。 组合的一个例子是汽车。 汽车由车轮、发动机和其他各种部件组成。
通过嵌入结构体进行组合
在 go 中可以通过将一种结构类型嵌入到另一种中来实现组合。
博客文章是一个完美的组合示例。 每篇博文都有标题、内容和作者信息。 这可以使用组合完美地表示。 在本篇内容的后续步骤中,我们将了解这是如何完成的。
首先我们创建一个 author 结构体
package main
import (
"fmt"
)
type author struct {
firstname string
lastname string
bio string
}
func (a author) fullname() string {
return fmt.sprintf("%s %s", a.firstname, a.lastname)
}
在上面的代码片段中,我们创建了一个包含 firstname、lastname 和 bio 字段的 author 结构体。 我们还添加了一个fullname() 方法,该方法接收一个 author类型的参数,它返回作者的全名。
下一步是创建 blogpost 结构体。
type blogpost struct {
title string
content string
author
}
func (b blogpost) details() {
fmt.println("title: ", b.title)
fmt.println("content: ", b.content)
fmt.println("author: ", b.author.fullname())
fmt.println("bio: ", b.author.bio)
}
blogpost 结构体具有 title、content 字段。 它还具有嵌入式匿名字段 author。 该字段表示 blogpost 结构体由 author 组成。 现在 blogpost 结构可以访问 author 结构的所有字段和方法。 我们还向 blogpost 结构添加了 details() 方法,该方法打印了作者的标题、内容、全名和简介。
每当一个结构体字段嵌入到另一个结构体中时,go 都为我们提供了访问嵌入字段的选项,就好像它们是外部结构体的一部分一样。 这意味着 p.author.fullname() 可以用 p.fullname() 替换。 因此 details() 方法可以重写如下,
func (p blogpost) details() {
fmt.println("title: ", p.title)
fmt.println("content: ", p.content)
fmt.println("author: ", p.fullname())
fmt.println("bio: ", p.bio)
}
现在我们已经准备好了 author 和 blogpost 结构体,让我们通过创建一个博客文章来完成这个程序。
创建博客文章package main import ( "fmt" ) type author struct { firstname string lastname string bio string } func (a author) fullname() string { return fmt.sprintf("%s %s", a.firstname, a.lastname) } type blogpost struct { title string content string author } func (b blogpost) details() { fmt.println("title: ", b.title) fmt.println("content: ", b.content) fmt.println("author: ", b.fullname()) fmt.println("bio: ", b.bio) } func main() { author1 := author{ "naveen", "ramanathan", "golang enthusiast", } blogpost1 := blogpost{ "inheritance in go", "go supports composition instead of inheritance", author1, } blogpost1.details() }
该程序运行结果如下

嵌入结构体切片
我们可以更进一步地使用这个示例,并使用一个博客文章的e)创建一个网站:)。
让我们先定义 website 结构体。 在现有程序的主函数上方添加以下代码并运行它。
type website struct {
[]blogpost
}
func (w website) contents() {
fmt.println("contents of website\n")
for _, v := range w.blogposts {
v.details()
fmt.println()
}
}
添加上面代码后运行上面的程序时,编译器会如下错误
main.go:31:9: syntax error: unexpected [, expecting field name or embedded type
此错误是由切片[]blogpost引起的。原因是不可能匿名嵌入切片。需要字段名。因此,让我们修复此错误并使编译器能编译通过。
type website struct {
blogposts []blogpost
}
添加字段 blogposts,它是一个切片 []blogposts。
现在让我们修改 main 函数并为我们的新网站创建一些帖子。
修改main函数后的完整程序如下package main import ( "fmt" ) type author struct { firstname string lastname string bio string } func (a author) fullname() string { return fmt.sprintf("%s %s", a.firstname, a.lastname) } type blogpost struct { title string content string author } func (p blogpost) details() { fmt.println("title: ", p.title) fmt.println("content: ", p.content) fmt.println("author: ", p.fullname()) fmt.println("bio: ", p.bio) } type website struct { blogposts []blogpost } func (w website) contents() { fmt.println("contents of website\n") for _, v := range w.blogposts { v.details() fmt.println() } } func main() { author1 := author{ "naveen", "ramanathan", "golang enthusiast", } blogpost1 := blogpost{ "inheritance in go", "go supports composition instead of inheritance", author1, } blogpost2 := blogpost{ "struct instead of classes in go", "go does not support classes but methods can be added to structs", author1, } blogpost3 := blogpost{ "concurrency", "go is a concurrent language and not a parallel one", author1, } w := website{ blogposts: []blogpost{blogpost1, blogpost2, blogpost3}, } w.contents() }
运行结果如下

在上面的 main 函数中,我们创建了一个作者 author1 和三个帖子 post1、post2 和 post3。 最后,我们中创建了网站 w。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
在 javascript 中验证 google recaptcha 第 2 版
发布时间:2024/03/23 浏览次数:193 分类:javascript
-
本文演示了如何在 javascript 中验证 google recaptcha。
c# 中的 goto 语句
发布时间:2024/02/02 浏览次数:184 分类:编程语言
-
本教程演示了如何在 c# 中使用 goto 以及何时使用它会有所帮助本教程将演示如何在 c# 中使用 goto 语法,并提供一些代码中的实际使用示例。
发布时间:2023/12/20 浏览次数:197 分类:python
-
本文为你提供了 python 中是否存在 goto 语句的答案。本文为你提供了 python 中是否存在 goto 语句的答案。基本上,python 不支持 goto 语句。
避免 python中的 typeerror: input expected at most 1 argument, got 3 错误
发布时间:2023/07/08 浏览次数:671 分类:python
-
python 中避免 typeerror: input expected atmost 1 argument, got 3 error在python编程中,我们有两个内置方法来获取用户的输入:input(prompt)和 raw_input(prompt)。
使用 python 将文件上传到 google 云端硬盘
发布时间:2023/06/15 浏览次数:544 分类:python
-
本文将介绍我们如何使用 python 将文件上传到云盘,以 google drive 为例。 为此,我们将使用 google drive api。
发布时间:2023/05/30 浏览次数:293 分类:python
-
当我们在 numpy 中传递一维数组而不是二维数组时,会发生错误 valueerror: expected 2d array, got 1d array instead 。如您所知,每种编程语言都会遇到很多错误,有些是在运行时,有些是在编译时。 pyth

