英文:
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() //无法运行
}
这段代码的作用是:
- 定义了三个结构体类型:
obj1
、obj1Selector
和obj2
。 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}
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论