2,190
社区成员
发帖
与我相关
我的任务
分享
package main
import (
"fmt"
)
type StructA struct {
}
type StructB struct {
StructA
}
type StructInterface interface {
Foo()
Call()
}
func (s *StructA) Foo() {
fmt.Println("StructA")
}
func (s *StructB) Foo() {
fmt.Println("StructB")
}
func (s *StructA) Call() {
Call(s)
}
func (s *StructB) Call() {
Call(s)
}
func Call(s StructInterface) {
s.Foo()
}
func main() {
var value StructInterface
value = new(StructB)
value.Foo()
value.Call()
}
上面代码里,structB实现了Foo和Call两个方法,所以,value.Foo()与value.Call()都会在B的scope里找到,输出两个structB,但是如何注释掉下面的代码
//func (s *StructB) Call() {
// Call(s)
//}
那么B没有实现Call这个方法,会去它的子scope,也就是structA里面寻找,然后输出A的实现