GO – How does an implicit method work?

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

GO - How does an implicit method work?

问题

在Go语言中,我学到了以下内容:

  1. 程序员只能在具名类型(X)或指向具名类型的指针(*X)上定义方法。

  2. 对于类型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

  1. 对于类型X的显式方法定义隐式地为类型*X定义了相同的方法,反之亦然。

这是不正确的。方法不是隐式定义的。编译器为你做的唯一事情是为了方便,隐式地将c.foo()替换为(*c).foo()c.foo()替换为(&c).foo()。请参考Go之旅

  1. 方法的接收者可以是类型T或类型*T,取决于你的显式声明。
英文:
  1. 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

  1. The receiver of the method is either of type T or of type *T, based on your explicit declaration.

huangapple
  • 本文由 发表于 2017年1月29日 15:52:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/41918750.html
匿名

发表评论

匿名网友

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

确定