当前位置:网站首页>Embedded struct and embedded interface
Embedded struct and embedded interface
2022-06-21 12:10:00 【attempt_ to_ do】
The embedded struct
When anonymous field is a struct When , So this struct All fields owned are implicitly introduced into the currently defined struct.
package main
import "fmt"
type Human struct {
name string
age int
weight int
}
type Student struct {
Human // Anonymous field , So default Student contains Human All fields of
speciality string
}
func main() {
// We initialize a student
mark := Student{
Human{
"Mark", 25, 120}, "Computer Science"}
// We go to the corresponding fields
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)
// Modify the corresponding notes
mark.speciality = "AI"
fmt.Println("Mark changed his speciality")
fmt.Println("His speciality is ", mark.speciality)
// Modify his age information
fmt.Println("Mark become old")
mark.age = 46
fmt.Println("His age is", mark.age)
// Modify his weight information
fmt.Println("Mark is not an athlet anymore")
mark.weight += 60
fmt.Println("His weight is", mark.weight)
}
The embedded interface
Go What's really fascinating about it is its built-in logic and Syntax , It's like we're learning Struct Anonymous fields learned when , How elegant , So the same logic is introduced to interface Inside , That's not more perfect . If one interface1 As interface2 An embedded field of , that interface2 Implicit inclusion interface1 Inside method.
We can see the source package container/heap There is such a definition in it
type Interface interface {
sort.Interface // Embed fields 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
}
We see sort.Interface It's actually embedded fields , hold sort.Interface All of the method Implicit inclusion . That is to say, the following three methods :
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)
}
边栏推荐
- 记一次Vmware虚拟机升级GLIBC导致系统瘫痪的恢复解决方法
- i.MX - RT1052时钟及锁相环(PLL)分析
- Is 100W data table faster than 1000W data table query?
- 1108. IP address invalidation
- SSD [target detection]
- Flink tuning (I) resource tuning and back pressure analysis
- 对文件夹下所有的文件一键改名
- 创建型模式 - 单例模式
- 重磅,MapStruct 1.5 发布,这次终于支持Map转为Bean了!
- 浅论OCA\UV-OCA LOCA\SLOCA 四种全贴合工艺
猜你喜欢
随机推荐
2022 HV electrician judgment questions and answers
findpanel的相关代码
Ansible 配置首次ssh免认证的操作说明
Second harmonyos training
SSD【目标检测篇】
Jenkins 通过Build periodically配置定时任务
Apache shardingsphere 5.1.2 release | new driving API + cloud native deployment to create a high-performance data gateway
养老年金险是理财产品吗?预期收益是多少?
【深度学习】利用深度学习监控女朋友的微信聊天?
Customization of power aging test system | overview of charging pile automatic test system nsat-8000
理解RESTful架构
STL基本容器测试
[deep learning] use deep learning to monitor your girlfriend's wechat chat?
记录一次pytorch训练模型遇到的报错
Jenkins configures scheduled tasks through build periodically
嵌入struct和嵌入interface
MySQL-DML
异质化社群量化研究4丨RATE OF CHANGE WITH BANDS
看懂UML类图和时序图
SSD [target detection]









