小编ren*_*xis的帖子

了解struct embedding

有人可以解释一下为什么这段代码打印1而不是2?

package main

import (
    "fmt"
)

type S1 struct{
    f1 string
}

type S2 struct{
    S1
    f2 string
}   

func (s *S1) Say(){
    fmt.Println("1")
}   

func (s *S2) Say(){
    fmt.Println("2")
}       

type S3 S2

func main() {
    var s3 S3
    s3.Say()
}
Run Code Online (Sandbox Code Playgroud)

(Runnable at:https://play.golang.org/p/_cjNxBKgSf)

methods struct go

1
推荐指数
1
解决办法
332
查看次数

标签 统计

go ×1

methods ×1

struct ×1