在Go语言中,一个类型和一个指向类型的指针都可以实现一个接口吗?

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

In Go, can both a type and a pointer to a type implement an interface?

问题

例如在下面的例子中:

  1. type Food interface {
  2. Eat() bool
  3. }
  4. type vegetable_s struct {
  5. //一些数据
  6. }
  7. type Vegetable *vegetable_s
  8. type Salt struct {
  9. // 一些数据
  10. }
  11. func (p Vegetable) Eat() bool {
  12. // 一些代码
  13. }
  14. func (p Salt) Eat() bool {
  15. // 一些代码
  16. }

VegetableSalt都满足Food接口吗,即使一个是指针,另一个是直接的结构体?

英文:

For example in the following example:

  1. type Food interface {
  2. Eat() bool
  3. }
  4. type vegetable_s struct {
  5. //some data
  6. }
  7. type Vegetable *vegetable_s
  8. type Salt struct {
  9. // some data
  10. }
  11. func (p Vegetable) Eat() bool {
  12. // some code
  13. }
  14. func (p Salt) Eat() bool {
  15. // some code
  16. }

Do Vegetable and Salt both satisfy Food, even though one is a pointer and the other is directly a struct?

答案1

得分: 15

The answer is easy to get by compiling the code:

  1. prog.go:19: invalid receiver type Vegetable (Vegetable is a pointer type)

The error is based on the specs requirement of:

> The receiver type must be of the form T or *T where T is a type name. The type denoted by T is called the receiver base type; it must not be a pointer or interface type and it must be declared in the same package as the method.

(Emphasizes mine)

The declaration:

  1. type Vegetable *vegetable_s

declares a pointer type, ie. Vegetable is not eligible as a method receiver.

英文:

The answer is easy to get by compiling the code:

  1. prog.go:19: invalid receiver type Vegetable (Vegetable is a pointer type)

The error is based on the specs requirement of:

> The receiver type must be of the form T or *T where T is a type name. The type denoted by T is called the receiver base type; it must not be a pointer or interface type and it must be declared in the same package as the method.

(Emphasizes mine)

The declaration:

  1. type Vegetable *vegetable_s

declares a pointer type, ie. Vegetable is not eligible as a method receiver.

答案2

得分: 0

你可以这样做:

  1. package main
  2. type Food interface {
  3. Eat() bool
  4. }
  5. type vegetable_s struct {}
  6. type Vegetable vegetable_s
  7. type Salt struct {}
  8. func (p *Vegetable) Eat() bool {return false}
  9. func (p Salt) Eat() bool {return false}
  10. func foo(food Food) {
  11. food.Eat()
  12. }
  13. func main() {
  14. var f Food
  15. f = &Vegetable{}
  16. f.Eat()
  17. foo(&Vegetable{})
  18. foo(Salt{})
  19. }
英文:

You could do the following:

  1. package main
  2. type Food interface {
  3. Eat() bool
  4. }
  5. type vegetable_s struct {}
  6. type Vegetable vegetable_s
  7. type Salt struct {}
  8. func (p *Vegetable) Eat() bool {return false}
  9. func (p Salt) Eat() bool {return false}
  10. func foo(food Food) {
  11. food.Eat()
  12. }
  13. func main() {
  14. var f Food
  15. f = &Vegetable{}
  16. f.Eat()
  17. foo(&Vegetable{})
  18. foo(Salt{})
  19. }

huangapple
  • 本文由 发表于 2013年7月28日 04:25:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/17902127.html
匿名

发表评论

匿名网友

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

确定