英文:
GO - How does an implicit method work?
问题
在Go语言中,我学到了以下内容:
-
程序员只能在具名类型(
X
)或指向具名类型的指针(*X
)上定义方法。 -
对于类型
X
的显式方法定义隐式地为类型*X
定义了相同的方法,反之亦然。因此,如果我声明了以下方法:
func (c Cat) foo(){
// 做一些事情
}
func (c *Cat) foo(){
// 做一些事情
}
那么Go编译器会报错,提示"方法重新声明",这表明指针方法和非指针方法是隐式定义的。
对于给定的具名类型Cat
,如下所示:
type Cat struct{
Name string
Age int
Children []Cat
Mother *Cat
}
场景1
程序员在具名类型Cat
上定义了方法foo
:
func (c Cat) foo(){
// 做一些事情
}
这隐式地为指向具名类型的指针*Cat
定义了方法foo
,由Go编译器自动生成的代码如下:
func (c *Cat) foo(){
// 做一些事情
}
当创建具名类型Cat
的变量时:
var c Cat
var p *Cat = &c
c.foo()
调用的是程序员定义的方法。
**问题1:**在调用p.foo()
时,隐式的指针方法是否接收指针p
?
场景2
程序员在指向具名类型的指针*Cat
上定义了方法foo
:
func (c *Cat) foo(){
// 做一些事情
}
这隐式地为具名类型Cat
定义了方法foo
,由Go编译器自动生成的代码如下:
func (c Cat) foo(){
// 做一些事情
}
当创建具名类型Cat
的变量时:
var c Cat
var p *Cat = &c
p.foo()
调用的是程序员定义的方法(上面的方法)。
**问题2:**在调用c.foo()
时,隐式的非指针方法是否接收值c
?
英文:
> In GO, I learnt that,
> 1)
> Programmer can only define methods on named types(X
) or pointer(*X
) to named types
> 2)
> An explicit method definition for type X
implicitly defines the same method for type *X
and vice versa, so, my understanding is, If I declare,
> func (c Cat) foo(){
//do stuff_
}
> and declare,
> func (c *Cat) foo(){
// do stuff_
}
> then GO compiler gives,
> Compile error: method re-declared
> which indicates that, pointer method is implicitly defined and vice versa
> With the given named type(Cat
),
> type Cat struct{
Name string
Age int
Children []Cat
Mother *Cat
}
> Scenario 1
> Method(foo
) defined on a named type(Cat
), by programmer,
> func (c Cat) foo(){
// do stuff....
}
> that implicitly defines method(foo
) on pointer(*Cat
) to named type, by GO compiler, that looks like,
> func (c *Cat) foo(){
// do stuff....
}
> On creating variables of named type(Cat
)
> var c Cat
var p *Cat = &c
> c.foo()
has the method defined by programmer.
> Question 1:
> On invoking p.foo()
, does implicit pointer method receive the pointer(p
)?
> Scenario 2
> Method(foo
) defined on a pointer(*Cat
) to named type, by the programmer,
> func (c *Cat) foo(){
// do stuff....
}
> that implicitly defines method(foo
) on named type(Cat
), by the GO compiler, that looks like,
> func (c Cat) foo(){
// do stuff....
}
> On creating variables of named type(Cat
)
> var c Cat
var p *Cat = &c
> p.foo()
has method defined by programmer(above).
> Question 2:
> On invoking c.foo()
, does the implicit non-pointer method receive the value c
?
答案1
得分: 5
- 对于类型X的显式方法定义隐式地为类型*X定义了相同的方法,反之亦然。
这是不正确的。方法不是隐式定义的。编译器为你做的唯一事情是为了方便,隐式地将c.foo()
替换为(*c).foo()
或c.foo()
替换为(&c).foo()
。请参考Go之旅。
- 方法的接收者可以是类型T或类型*T,取决于你的显式声明。
英文:
An explicit method definition for type X implicitly defines the same method for type *X and vice versa.
This is not correct. Methods are not defined implicitly. The only thing that a compiler does for you is implicitly replacing c.foo()
with (*c).foo()
or c.foo()
with (&c).foo()
for convenience. See the tour of Go
- The receiver of the method is either of type T or of type *T, based on your explicit declaration.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论