为了账号安全,请及时绑定邮箱和手机立即绑定

Golang 继承和方法覆盖

Golang 继承和方法覆盖

Go
HUWWW 2023-03-29 15:25:06
澄清:我刚刚学习围棋,遇到了这个问题。我正在尝试实现一个“类”,它继承了一个调用应该由子类实现的“虚拟”方法的方法。这是我的代码:https://play.golang.org/p/ysiaPwARkvlpackage mainimport (    "fmt"    "sync")type Parent struct {  sync.Mutex  MyInterface}func (p *Parent) Foo() {  p.Lock()  defer p.Unlock()  p.Bar()}func (p *Parent) B(){  panic("NOT IMPLEMENTED")}func (p *Parent) A() {  p.Lock()  defer p.Unlock()  p.B()}type MyInterface interface {  Foo()  Bar()}type Child struct {  Parent  Name string}func (c *Child) Bar(){  fmt.Println(c.Name)}func (c *Child) B(){  fmt.Println(c.Name)}func main() {  c := new(Child)  c.Name = "Child"  // c.A() panic  c.Foo() // pointer error}我遗漏了一些关于 sync.Mutex 的代码,这些代码对 Child 的值进行一些异步更新。所以显然在 A() 或 Foo() 中,指针 p 的类型为 Parent。我应该如何更改我的代码,以便 A/Foo 引用 Child 类中定义的 B/Bar?
查看完整描述

1 回答

?
慕丝7291255

TA贡献1859条经验 获得超6个赞

当 Go 只提供has-a关系(组合)时,你想要一个is -a关系(继承):

  • Go 没有继承,因此两种类型之间 不存在is-a关系。Child不是一种Parent,所以指向 a 的指针Parent不能保留指向 a 的指针ChildChild has-a Parent包含在其中。

因为和 之间没有is-a关系,所以不能接收 类型的对象,也不能使用任何实现的方法。此外,这意味着没有人可以直接访问在such上定义的任何方法。ParentChildParent.FooChildChildParentChildBar()B()

通常,Parent不需要调用Child. 如果是这样,您将向该Parent方法传递一个参数,例如Child满足您通过接口调用该方法的接口或调用该Child方法的闭包:

// Use of an interface that Child satisfies.

type Beta interface {

    B()

}

func (p *Parent) A(b Beta) {

    p.Lock()

    defer p.Unlock()

    b.B()

}


// Use of a closure.

func (p *Parent) Foo(bar func()) {

    p.Lock()

    defer p.Unlock()

    bar()

}

func callFoo(p *Parent, c *Child) {

    callBar := func() {

        c.Bar()

    }

    p.Foo(callBar)

}


func (c *Child) Bar() {

    // stub

}


func (c *Child) B() {

    // stub

}

您可以免费获得Childcan callParent方法行为,但它只是看起来类似于继承。child.Foo()actually performs child.Parent.Foo(),这意味着Parent.Foo仍然接收一个Parent实例(因此得名),而不是一个Child实例。

但是,无法访问任何未明确共享Parent的信息。接口和闭包可以充当两个类之间的机制,类似于C++ 中的关键字,只是它们比关键字更具限制性。毕竟,不需要共享所有东西,只是它想共享的位,有点类似于C++ 中的这种模式。就我个人而言,我更喜欢接口,因为它允许您的“类”使用多种类型,这些类型都满足一个公共接口,使其与从普通函数或完全不相关的类型的方法调用方法几乎相同。ChildChildfriendfriendChildParentParent


查看完整回答
反对 回复 2023-03-29
  • 1 回答
  • 0 关注
  • 167 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信