下面代码输出什么?
type A interface {
ShowA() int
}
type B interface {
ShowB() int
}
type Work struct {
i int
}
func (w Work) ShowA() int {
return w.i + 10
}
func (w Work) ShowB() int {
return w.i + 20
}
func main() {
c := Work{3}
var a A = c
var b B = c
fmt.Println(a.ShowB())
fmt.Println(b.ShowA())
}
- A. 23 13
- B. compilation error
打卡
Mark.
打卡
mark
di~
mark
mark
mark
mark
编译不通过吧?(golang码农求职)
mark
打卡
打卡
就是说,A接口没有ShowB方法,B接口也没有ShowA方法
打卡
fmt.Println(a.ShowA()) ==> 13 fmt.Println(b.ShowB()) ==> 23
打卡。。。。
mk
mk
关键:接口的静态类型和动态类型(piar内包含type与data),a、b 具有相同的动态类型和动态值,分别是结构体 work 和 {3};a 的静态类型是 A,b 的静态类型是 B,接口 A 不包括方法 ShowB(),接口 B 也不包括方法 ShowA(),编译报错。
done
没看清楚调用的方法
1
打卡学习
打卡学习!
打卡
打卡
mark
mark
还需要注意接收者的类型 cannot use c (variable of type Work) as type A in variable declaration: Work does not implement A (ShowA method has pointer receiver)
打卡
吗
mark
mark
mark
mark
卡
打卡
m
打卡 还需要注意接收者的类型 cannot use c (variable of type Work) as type A in variable declaration: Work does not implement A (ShowA method has pointer receiver)
打卡
a,b具有相同的动态类型和动态值,分别是结构体work和{3},a的静态类型是A,b的静态类型是B,接口A不包括showB方法,同样,接口B也不包括showA方法