理解接口

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

Understanding Interface

问题

type Info interface {
Noofchar() int
}

type Testinfo struct {
noofchar int
}

func (x Testinfo)Noofchar() int {
return x.noofchar
}

func main(){
var t Info
fmt.Println(x.Testinfo)
fmt.Println("No of char ",t.Noofchar())
x.noofchar++
fmt.Println("No of char ",t.Noofchar())
}

英文:

I am a newbie in Go lang , I was trying to understand Go Interface by writing a simple piece of code . I am getting an error , as I am not able to understand the correct way of referring to a Interface method kindly tell me where I am going wrong.

  1. type Info interface {
  2. Noofchar() int
  3. }
  4. type Testinfo struct {
  5. noofchar int
  6. }
  7. func (x Testinfo)Noofchar() int {
  8. return x.noofchar
  9. }
  10. func main(){
  11. var t Info
  12. fmt.Println(x.Testinfo)
  13. fmt.Println("No of char ",t.Noofchar())
  14. x.noofchar++
  15. fmt.Println("No of char ",t.Noofchar())
  16. }

Am I referring to the method correctly by t.Noofchar() ? or there is something else that I am missing

答案1

得分: 3

方法通常接收结构体的指针。

  1. func (x Testinfo)Noofchar() int {

改为

  1. func (x *Testinfo)Noofchar() int {

在开头删除了var x Info,稍微重构了你的main()函数,得到的代码如下:

  1. package main
  2. import "fmt"
  3. type Info interface {
  4. Noofchar() int
  5. Increment()
  6. }
  7. type Testinfo struct {
  8. noofchar int
  9. }
  10. func (x *Testinfo) Noofchar() int {
  11. return x.noofchar
  12. }
  13. func (x *Testinfo) Increment() {
  14. x.noofchar++
  15. }
  16. func main(){
  17. var t Info = &Testinfo{noofchar:1}
  18. fmt.Println("No of char ",t.Noofchar())
  19. t.Increment()
  20. fmt.Println("No of char ",t.Noofchar())
  21. }

http://play.golang.org/p/6D-LzzYYMU

在你的示例中,你直接修改了x。如果你在传递接口时,你无法访问底层的数据结构,只能访问方法。所以我将你的直接递增改为了Increment()方法。

英文:

Methods typically receive pointers to a struct.

  1. func (x Testinfo)Noofchar() int {

changed to

  1. func (x *Testinfo)Noofchar() int {

Took out var x Info in the beginning, refactored your main() just a bit and the resulting code in play is:

  1. package main
  2. import "fmt"
  3. type Info interface {
  4. Noofchar() int
  5. Increment()
  6. }
  7. type Testinfo struct {
  8. noofchar int
  9. }
  10. func (x *Testinfo) Noofchar() int {
  11. return x.noofchar
  12. }
  13. func (x *Testinfo) Increment() {
  14. x.noofchar++
  15. }
  16. func main(){
  17. var t Info = &Testinfo{noofchar:1}
  18. fmt.Println("No of char ",t.Noofchar())
  19. t.Increment()
  20. fmt.Println("No of char ",t.Noofchar())
  21. }

http://play.golang.org/p/6D-LzzYYMU

In your example you modify x directly. If you're passing an interface around, you don't have access to the underlying data structures, only the methods. So I changed your direct increment to an Increment() method.

答案2

得分: 1

x是一个变量,你可以将任何实现Info接口的东西赋值给它。你还没有给这个变量赋值。

一旦你赋值了某个东西,x.noofchar++将不起作用,因为x可以保存任何实现Info接口的东西,这意味着你只能访问由该接口定义的方法。接口不允许直接访问字段。

Info接口中唯一定义的方法是Noofchar() int方法,所以这是与存储在x中的值交互的唯一方式。

方法接收器(x Testinfo)定义的xvar x Info变量没有任何关系。这个x 确实可以直接访问结构字段。

t.Noofchar()的调用是正确的,将起作用。

英文:

x is a variable to which you can assign anything that implements the Info interface. You've assigned nothing to that variable.

Once you assign something, x.noofchar++ will not work because again x can hold anything that implements the Info interface, which means you can only access the methods defined by that interface. Interfaces do not allow direct access to fields.

The only method defined in the Info interface is the Noofchar() int method, so that is the only way to interact with the value stored in x.

The x defined by the method receiver (x Testinfo) isn't at all related to the var x Info variable. That x does have direct access to the struct fields.

The t.Noofchar() calls are correct and will work.

huangapple
  • 本文由 发表于 2013年1月24日 05:28:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/14489702.html
匿名

发表评论

匿名网友

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

确定