Go语言自学系列 | golang继承
2023-09-14 09:09:08 时间
视频来源:B站《golang入门到项目实战 [2021最新Go语言教程,没有废话,纯干货!持续更新中...]》
一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!
附上汇总贴:Go语言自学系列 | 汇总_COCOgsta的博客-CSDN博客
golang本质上没有oop的概念,也没有继承的概念,但是可以通过结构体嵌套实现这个特性。
例如
package main
import "fmt"
type Animal struct {
name string
age int
}
func (a Animal) eat() {
fmt.Println("eat...")
}
func (a Animal) sleep() {
fmt.Println("sleep")
}
type Dog struct {
Animal
}
type Cat struct {
Animal
}
func main() {
dog := Dog{
Animal{
name: "dog",
age: 2,
},
}
cat := Cat{
Animal{
name: "cat",
age: 3,
},
}
dog.eat()
dog.sleep()
cat.eat()
dog.sleep()
}
运行结果
[Running] go run "/Users/guoliang/SynologyDrive/软件开发/go/golang入门到项目实战/goproject/360duote.com/pro01/test.go"
eat...
sleep
eat...
sleep
相关文章
- JetBrains GoLand 2022 Mac中文激活版(GO语言编程软件)
- Golang语言(打卡第四天)fmt格式化工具|Go主题月
- golang go语言 反向 websocket 代理演示代码
- Go 编程 | 连载 15 - Go 语言的函数
- go语言IP转换uint32,uint32转换IP「建议收藏」
- Go 1.18.1 Beta 尝鲜
- Go语言编译链接过程
- 在线客服系统源码编译Golang语言下go build命令构建二进制程序
- Go语言之闭包篇
- Go语言defer(延迟执行语句)
- go pprof命令(Go语言性能分析命令)完全攻略
- Go语言圣经-匿名函数习题详解编程语言
- go语言变量作用域详解编程语言
- Go语言网络爬虫中的基本数据结构
- Go语言函数值传递和引用传递
- Go语言通过内存缓存来提升性能
- 数据库探索Go语言与Oracle数据库的结合(go语言中的oracle)