golang中值类型的嵌入式字段和指针类型的嵌入式字段
2024-08-27 16:24:03
总结:
1. 值类型的嵌入式字段,该类型拥有值类型的方法集,没有值指针类型的方法集
2. 指针类型的嵌入式字段,该类型拥有值指针类型的方法集,没有值类型的方法集,并且,该类型的指针类型也有值指针类型的方法集
有点绕,见案例:
package main import "fmt" type Boss struct{}
func (b *Boss) AssignWork() {
fmt.Println("Boss assigned work")
} type Manager struct{}
func (m *Manager) PreparePowerPoint() {
fmt.Println("PowerPoint prepared")
} type BossManager struct {
Boss // 嵌入式字段
Manager // 嵌入式字段
}
type BossManagerUsingPointers struct {
*Boss // 指针类型的嵌入式字段
*Manager // 指针类型的嵌入式字段
}
/*
BossManager和BossManagerUsingPointers的区别?
BossManage类型没有实现这个PromotionMaterial接口,BossManage的指针类型实现了这个接口
BossManagerUsingPointers类型实现了PromotionMaterial这个接口
BossManagerUsingPointers的指针类型也是PromotionMaterial这个接口类型
*/ // Define an interface that requires both methods.
type PromotionMaterial interface {
AssignWork()
PreparePowerPoint()
} func promote(pm PromotionMaterial) {
fmt.Println("Promoted a person with promise.")
} func main() {
bm := BossManager{} // Both methods (which use pointer receivers) have been promoted to BossManager.
bm.AssignWork() // "Boss assigned work"
bm.PreparePowerPoint() // "PowerPoint prepared" // However, the method set of BossManager does not include either method because:
// 1) {Boss, Manager} are embedded as value types, not pointer types.
// 2) This makes it so that only the pointer type *BossManager includes both methods
// in its method set, thus making it implement interface PromotionMaterial. // promote(bm) // Would fail with: cannot use bm (type BossManager) as type PromotionMaterial in argument to promote:
// BossManager does not implement PromotionMaterial (AssignWork method has pointer receiver)
// This would work if {Boss, Manager} were embedded as pointer types. promote(&bm) // Works // Lets use the struct with the embedded pointer types:
bm2 := BossManagerUsingPointers{}
bm2.AssignWork() // "Boss assigned work"
bm2.PreparePowerPoint() // "PowerPoint prepared"
promote(bm2) // Works
promote(&bm2) // Also works, since both BossManagerUsingPointers (value type) and *BossManagerUsingPointers (pointer type)
// method sets include both methods.
}
参考网址:https://unitstep.net/blog/2015/09/16/golang-promoted-methods-method-sets-and-embedded-types/
最新文章
- vim编辑强制退出
- Ubuntu16.04 安装配置Caffe
- 关于C#循环图片GDI+内存不足异常的记录
- gulp 制作雪碧图
- 分析php获取客户端ip
- 约瑟夫问题--list模拟循环链表
- Linux - 简明Shell编程06 - 循环语句(Loop)
- python空字典列表两种生成方式对赋值带来的不同影响
- 【Uva 11280 飞到弗雷德里顿】
- 设计模式学习心得<;适配器 Adapter>;
- Android gradle实现多渠道号打包
- python列表的11种方法
- PHP + Redis 队列实战
- 链栈的基本操作(C语言)
- 百度Web Uploader组件实现文件上传(一)
- Js_数组操作
- asp.net mvc 全局权限过滤器及继成权限方法
- .net 系列:Expression表达式树、lambda、匿名委托 的使用【转】
- Codeforces 447C - DZY Loves Sequences
- Leetcode题库——8.字符串转为整数【##】
热门文章
- JAVA接收postman的中raw的参数
- JAVA连接MySQ报错:Caused by: javax.net.ssl.SSLException: Received fatal alert: protocol_version
- 【LeetCode】1086. High Five 解题报告(C++)
- 【九度OJ】题目1442:A sequence of numbers 解题报告
- 【LeetCode】778. Swim in Rising Water 水位上升的泳池中游泳(Python)
- C. Watching Fireworks is Fun(Codeforces 372C)
- D. Persistent Bookcase(Codeforces Round #368 (Div. 2))
- node.js安装及环境配置超详细教程【Windows系统安装包方式】
- Ubuntu18.04 + Windows10 双系统安装
- Ubuntu18.04安装/卸载NVIDIA显卡驱动