总结:

  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/

最新文章

  1. vim编辑强制退出
  2. Ubuntu16.04 安装配置Caffe
  3. 关于C#循环图片GDI+内存不足异常的记录
  4. gulp 制作雪碧图
  5. 分析php获取客户端ip
  6. 约瑟夫问题--list模拟循环链表
  7. Linux - 简明Shell编程06 - 循环语句(Loop)
  8. python空字典列表两种生成方式对赋值带来的不同影响
  9. 【Uva 11280 飞到弗雷德里顿】
  10. 设计模式学习心得<适配器 Adapter>
  11. Android gradle实现多渠道号打包
  12. python列表的11种方法
  13. PHP + Redis 队列实战
  14. 链栈的基本操作(C语言)
  15. 百度Web Uploader组件实现文件上传(一)
  16. Js_数组操作
  17. asp.net mvc 全局权限过滤器及继成权限方法
  18. .net 系列:Expression表达式树、lambda、匿名委托 的使用【转】
  19. Codeforces 447C - DZY Loves Sequences
  20. Leetcode题库——8.字符串转为整数【##】

热门文章

  1. JAVA接收postman的中raw的参数
  2. JAVA连接MySQ报错:Caused by: javax.net.ssl.SSLException: Received fatal alert: protocol_version
  3. 【LeetCode】1086. High Five 解题报告(C++)
  4. 【九度OJ】题目1442:A sequence of numbers 解题报告
  5. 【LeetCode】778. Swim in Rising Water 水位上升的泳池中游泳(Python)
  6. C. Watching Fireworks is Fun(Codeforces 372C)
  7. D. Persistent Bookcase(Codeforces Round #368 (Div. 2))
  8. node.js安装及环境配置超详细教程【Windows系统安装包方式】
  9. Ubuntu18.04 + Windows10 双系统安装
  10. Ubuntu18.04安装/卸载NVIDIA显卡驱动