可以让调用父方法的孩子进入吗?

时间:2018-09-21 07:47:56

标签: go struct

假设我们有这样的代码:

package main

import "fmt"

type Parent struct{}

func (p Parent) WhoCalledMe() string {
    // return the name of instance who called this method, Parent or Child
}

type Child struct {
    Parent
}

func main() {
    child := Child{}
    parent := Parent{}
    fmt.Println(child.WhoCalledMe()) // return Child
    fmt.Println(parent.WhoCalledMe()) // return Parent
}

是否可以获取调用WhoCalledMe方法的实例的名称或类型?

0 个答案:

没有答案