package main
import "fmt"
type People struct{}
func (p *People) ShowA() {
fmt.Println("showA")
p.ShowB()
}
func (p *People) ShowB() {
fmt.Println("showB")
}
type Teacher struct {
People
}
func (t *Teacher) ShowB() {
fmt.Println("teacher showB")
}
func main() {
t := Teacher{}
t.ShowA()
}
如上代码,为什么输出的是:
showA
showB
而不是:
showA
teacher showB
因为Golang中没有继承的概念,这里实际上只是结构体类型的嵌入,被嵌入的类型是无法直接访问嵌入者的成员或者是方法的。