当前位置:网站首页>嵌入struct和嵌入interface
嵌入struct和嵌入interface
2022-06-21 12:00:00 【attempt_to_do】
嵌入struct
当匿名字段是一个struct的时候,那么这个struct所拥有的全部字段都被隐式地引入了当前定义的这个struct。
package main
import "fmt"
type Human struct {
name string
age int
weight int
}
type Student struct {
Human // 匿名字段,那么默认Student就包含了Human的所有字段
speciality string
}
func main() {
// 我们初始化一个学生
mark := Student{
Human{
"Mark", 25, 120}, "Computer Science"}
// 我们访问相应的字段
fmt.Println("His name is ", mark.name)
fmt.Println("His age is ", mark.age)
fmt.Println("His weight is ", mark.weight)
fmt.Println("His speciality is ", mark.speciality)
// 修改对应的备注信息
mark.speciality = "AI"
fmt.Println("Mark changed his speciality")
fmt.Println("His speciality is ", mark.speciality)
// 修改他的年龄信息
fmt.Println("Mark become old")
mark.age = 46
fmt.Println("His age is", mark.age)
// 修改他的体重信息
fmt.Println("Mark is not an athlet anymore")
mark.weight += 60
fmt.Println("His weight is", mark.weight)
}
嵌入interface
Go里面真正吸引人的是它内置的逻辑语法,就像我们在学习Struct时学习的匿名字段,多么的优雅啊,那么相同的逻辑引入到interface里面,那不是更加完美了。如果一个interface1作为interface2的一个嵌入字段,那么interface2隐式的包含了interface1里面的method。
我们可以看到源码包container/heap里面有这样的一个定义
type Interface interface {
sort.Interface //嵌入字段sort.Interface
Push(x interface{
}) //a Push method to push elements into the heap
Pop() interface{
} //a Pop elements that pops elements from the heap
}
我们看到sort.Interface其实就是嵌入字段,把sort.Interface的所有method给隐式的包含进来了。也就是下面三个方法:
type Interface interface {
// Len is the number of elements in the collection.
Len() int
// Less returns whether the element with index i should sort
// before the element with index j.
Less(i, j int) bool
// Swap swaps the elements with indexes i and j.
Swap(i, j int)
}
边栏推荐
- 看懂UML类图和时序图
- 2022 safety officer-b certificate retraining question bank and simulated examination
- One's deceased father grind politics English average cent furnace! What is your current level?
- Citus 11 for Postgres is completely open source and can be queried from any node (citus official blog)
- SDCC编译器 + VSCode开发 8位微控制器
- Chapter VIII web project testing
- 请问各位大佬,flink cdc在抽取oracle全量数据之前会加表级排他锁
- Compilation de l'environnement vs Code + GCC développé par stm32
- 使用Huggingface在矩池云快速加载预训练模型和数据集
- SSD [target detection]
猜你喜欢

蜜雪冰城(已黑化)

这3个后生,比马化腾、张一鸣还狠

Ansible operating instructions for configuring SSH authentication free for the first time

重磅,MapStruct 1.5 发布,这次终于支持Map转为Bean了!

巨头局终战:即时零售

i.MX - RT1052 BOOT启动

华为云发布桌面IDE-CodeArts

Interesting research on mouse pointer interaction

【无标题】

Illustrated with pictures and texts -- wechat applet to obtain the user's geographic location information and call Tencent map API to obtain the user's specific location
随机推荐
Typera free version, without cracking, can be installed and used directly
版本号命名规范
第八章 Web项目测试
Nanjing University static program analyses -- intermediate representation learning notes
ThinkPHP security development specification
Tensorflower uses the specified GPU and GPU video memory
事务Transaction
Sdcc compiler + vscode to develop 8-bit microcontroller
Travel does not heal the soul
一文搞懂 Flink OperatorChain 对象重用
这3个后生,比马化腾、张一鸣还狠
Est le logiciel d'oscilloscope allemand, le logiciel d'ordinateur hôte d'oscilloscope keysight NS scope
养老年金险是理财产品吗?预期收益是多少?
MySQL-DQL
typora免费版,无需破解,安装直接使用
浅论OCA\UV-OCA LOCA\SLOCA 四种全贴合工艺
Brief discussion on four full bonding processes of oca\uv-oca loca\sloca
i.MX - RT1052 SPI和 I2C接口
[deep learning] use deep learning to monitor your girlfriend's wechat chat?
findpanel的相关代码