小编Ant*_*wan的帖子

如何在 Golang 中调用父方法并传递任何扩展父结构作为参数的子方法

我还在学习Golang,想请教一些事情。是否可以执行类似的操作并将任何其他子级传递给扩展 Parent 结构的 PMethod ?

type Parent struct{
   PAttribute string
}

func (p *Parent) PMethod(c *Child){
   fmt.Println("this is parent Attribute : " + p.PAttribute)
   fmt.Println("this is child Attribute : " + c.CAttribute)
}

type Child struct{
   Parent
   CAttribute string
}

type Child2 struct{
   Parent
   CAttribute string
}


func main(){
   c := Child{
         Parent{
            "parent"
         },
         "child",
        }
   c.PMethod(&c)

   c2 := Child2{
         Parent{
            "parent"
         },
         "child",
        }
   c2.PMethod(&c2)
}
Run Code Online (Sandbox Code Playgroud)

谢谢

composition go

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

标签 统计

composition ×1

go ×1