Go面向对象编程6——嵌入 interface

在前面的课程中我们已经知道在结构体中可以嵌入匿名字段,其实在接口里也可以再嵌入接口。如果一个 interface1 作为 interface2 的一个嵌入字段,那么 interface2 隐式的包含了 interface1 里的方法。如下例子中,Interface2 包含了 Interface1 的所有方法。

1
2
3
4
5
6
7
8
9
type Interface1 interface {
Send()
Receive()
}

type Interface2 interface {
Interface1
Close()
}

Go面向对象编程6——嵌入 interface
https://hodlyounger.github.io/B_Code/GO/Go简明手册/面向对象编程/嵌入interface/【Go简明手册】Go面向对象编程6——嵌入 interface/
作者
mingming
发布于
2023年10月27日
许可协议