golang 组合和接口
lzp2011150309 · · 11157 次点击 · · 开始浏览一,组合
golang 中一切 皆是类型
这个和 面向对象的 概念有点像 ,但是又不太像,和javascript倒是有些相似之处
那么如何实现 像 Java中的 继承呢,golang使用的是组合
请看代码 和 运行输出 说明一切
type father struct {name string
sex int
}
type sun struct {
father
name string
}
func main() {
s:=sun{father:father{name:"father",sex:11},name:"sun"}
fmt.Println(s)
fmt.Println("name",s.name)
fmt.Println("name",s.father.name)
fmt.Println("sex",s.sex)
fmt.Println("sex",s.father.sex)
}
=======================================
{{father 11} sun}
name sun
name father
sex 11
sex 11
===================================
father的sex属性就被组合进了sun ,成为了sun的属性,虽然我的 命名是 father和 sun但 二者不是继承 是组合的关系
当father的属性 和 sun的属性同名,被组合者 即母体 sun优先,想要 访问 father的name需要 s.father.name
二 .接口
type Reader interface {
read()
}
type Writer interface {
write()
}
//定义上述两个接口的实现类
type MyReadWrite struct{}
func (mrw *MyReadWrite) read() {
fmt.Println("MyReadWrite...read")
}
func (mrw *MyReadWrite) write() {
fmt.Println("MyReadWrite...write")
}
//定义一个接口,组合了上述两个接口
type ReadWriter interface {
Reader
Writer
}
//上述接口等价于:
type ReadWriterV2 interface {
read()
write()
}
//ReadWriter和ReadWriterV2两个接口是等效的,因此可以相互赋值
func interfaceTest0104() {
mrw := &MyReadWrite{}
//mrw对象实现了read()方法和write()方法,因此可以赋值给ReadWriter和ReadWriterV2
var rw1 ReadWriter = mrw
rw1.read()
rw1.write()
fmt.Println("------")
var rw2 ReadWriterV2 = mrw
rw2.read()
rw2.write()
//同时,ReadWriter和ReadWriterV2两个接口对象可以相互赋值
rw1 = rw2
rw2 = rw1
}
三.接口和组合
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
一,组合
golang 中一切 皆是类型
这个和 面向对象的 概念有点像 ,但是又不太像,和javascript倒是有些相似之处
那么如何实现 像 Java中的 继承呢,golang使用的是组合
请看代码 和 运行输出 说明一切
type father struct {name string
sex int
}
type sun struct {
father
name string
}
func main() {
s:=sun{father:father{name:"father",sex:11},name:"sun"}
fmt.Println(s)
fmt.Println("name",s.name)
fmt.Println("name",s.father.name)
fmt.Println("sex",s.sex)
fmt.Println("sex",s.father.sex)
}
=======================================
{{father 11} sun}
name sun
name father
sex 11
sex 11
===================================
father的sex属性就被组合进了sun ,成为了sun的属性,虽然我的 命名是 father和 sun但 二者不是继承 是组合的关系
当father的属性 和 sun的属性同名,被组合者 即母体 sun优先,想要 访问 father的name需要 s.father.name
二 .接口
type Reader interface {
read()
}
type Writer interface {
write()
}
//定义上述两个接口的实现类
type MyReadWrite struct{}
func (mrw *MyReadWrite) read() {
fmt.Println("MyReadWrite...read")
}
func (mrw *MyReadWrite) write() {
fmt.Println("MyReadWrite...write")
}
//定义一个接口,组合了上述两个接口
type ReadWriter interface {
Reader
Writer
}
//上述接口等价于:
type ReadWriterV2 interface {
read()
write()
}
//ReadWriter和ReadWriterV2两个接口是等效的,因此可以相互赋值
func interfaceTest0104() {
mrw := &MyReadWrite{}
//mrw对象实现了read()方法和write()方法,因此可以赋值给ReadWriter和ReadWriterV2
var rw1 ReadWriter = mrw
rw1.read()
rw1.write()
fmt.Println("------")
var rw2 ReadWriterV2 = mrw
rw2.read()
rw2.write()
//同时,ReadWriter和ReadWriterV2两个接口对象可以相互赋值
rw1 = rw2
rw2 = rw1
}