为什么这种方法在Go语言中没有得到推广?

huangapple go评论85阅读模式
英文:

Why isn't this method promoted in Go?

问题

以下是代码的翻译:

type obj1 struct {
    obj2
}

type obj1Selector struct {
    selector obj2
}

type obj2 struct {
}

func (o obj2) printTest() {
    fmt.Println("obj2")
}

func main() {
    o := obj1{}
    o.printTest()

    oSelector := obj1Selector{}
    oSelector.selector.printTest()
    //oSelector.printTest() //无法运行
}

这段代码的作用是:

  • 定义了三个结构体类型:obj1obj1Selectorobj2
  • obj1结构体嵌入了obj2结构体,意味着obj1包含了obj2的所有字段和方法。
  • obj1Selector结构体中有一个名为selector的字段,其类型为obj2。这意味着obj1Selector并没有嵌入obj2,而是实现了一个名为selector的字段,该字段的类型是obj2
  • obj2结构体定义了一个名为printTest的方法,用于打印字符串"obj2"。
  • main函数中,首先创建了一个obj1类型的变量o,然后调用o.printTest()方法,输出"obj2"。
  • 接下来创建了一个obj1Selector类型的变量oSelector,然后通过oSelector.selector.printTest()调用了obj2结构体的printTest方法,同样输出"obj2"。
  • 最后一行的oSelector.printTest()无法运行,因为obj1Selector结构体本身并没有定义printTest方法,只有selector字段拥有该方法。

希望对你有帮助!如果还有其他问题,请随时提问。

英文:

How does the following code work? My understanding is that obj1 embeds obj2 and therefore printTest is promoted such that we can call obj1.printTest() instead of needing to call it using obj1.obj2.printTest().

What happens with obj1Selector (I'm not sure what the string is even called that's in front of obj2)? Does obj1Selector embed obj2? Someone told me that obj1Selector does not embed obj2 but implements a field called selector that has the type obj2, but what does that mean - why isn't it embed? Why can't I call oSelector.printTest()?

type obj1 struct {
    obj2
}

type obj1Selector struct {
    selector obj2
}

type obj2 struct {
}

func (o obj2) printTest() {
    fmt.Println("obj2")
}

func main() {
    o := obj1{}
    o.printTest()

    oSelector := obj1Selector{}
    oSelector.selector.printTest()
    //oSelector.printTest() //Doesn't work

答案1

得分: 2

它被称为“嵌入”。

根据Golang规范:

Go没有提供典型的、基于类型的子类概念,但它确实可以通过在结构体或接口中嵌入类型来“借用”实现的部分。

obj1的情况下,它是通过嵌入类型obj2来实现的,这意味着obj2能做的事情,obj1也能做。因此,你可以直接在obj1上调用printTest

而在obj1Selector的情况下,它定义了一个新字段,名为selector,其类型为obj2。因此,你必须在selector上调用printTest,而不是在obj1Selector上调用。

你不能直接在obj1Selector上调用它,因为这个方法属于该结构体中的另一个字段,即selector

这类似于在Java中,我们可以有一个类型为另一个类的类字段,所以如果你想调用在该字段上定义的函数,它将是{该类的实例}.{字段名}.{方法}

英文:

It's called embedding.

From Golang specification:

> Go does not provide the typical, type-driven notion of subclassing, but it does have the ability to “borrow” pieces of an implementation by embedding types within a struct or interface.

In case of obj1, it is embedding type obj2, which means, what obj2 can do, obj1 can do. So you can invoke printTest directly on obj1.

While in case of obj1Selector, it defines a new field called selector, which has type obj2, So you have to invoke printTest on the selector, and not on obj1Selector.

You cannot call it directly on the obj1Selector, because this method belongs to another field in that struct, namely selector.

It is similar to how in java, we can have a class field with type of another class, So If you want to invoke a function defined on that field it will be {InstanceOfThatClass}.{fieldName}.{method}.

huangapple
  • 本文由 发表于 2016年4月16日 22:40:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/36665520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定