每次学习并整理一个Golang的知识点,每天进步一点点。今天学习下go中的一个知识点:接口
日省吾身
- 判断下面代码能否正常执行?如果可以,写出输出结果?
package mainimport "fmt"// W 结构体Wtype W struct {n int}func (x W) showD() int {result := x.n + 10return result}func (x W) showB() int {result := x.n + 20return result}// D 接口Dtype D interface {showD() int}// B 接口Btype B interface {showB() int}func main() {w := W{3}var dog D = wvar boy B = wfmt.Println(dog.showD())fmt.Println(boy.showB())}
思考后看下方答案…
![c36c52384ba0eef9ac954c6b599f7ce5.png](https://i-blog.csdnimg.cn/blog_migrate/9b72f0bee17e70bf5854c04bb284984e.jpeg)