本文实例讲述了Go语言中接口组合的实现方法。分享给大家供大家参考。具体实现方法如下:
在Go语言中,可以在接口A中组合其它的一个或多个接口(如接口B、C),这种方式等价于在接口A中添加接口B、C中声明的方法。
复制代码 代码如下:
//接口中可以组合其它接口,这种方式等效于在接口中添加其它接口的方法
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
}
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语言程序设计有所帮助。