[关闭]
@phper 2018-03-13T10:10:31.000000Z 字数 6977 阅读 2429

17.方法

Golang

原文:https://golangbot.com/methods/


欢迎来到 Golang 系列教程的第 17 章。

什么是方法?

方法其实就是一个函数,在 func 这个关键字和方法名中间加入了一个特殊的接收器类型。接收器可以是结构体类型或者是非结构体类型。接收器是可以在方法的内部访问的。

下面就是创建一个方法的语法。

  1. func (t Type) methodName(parameter list) {
  2. }

上面的代码片段创建了一个接收器类型为 Type 的方法 methodName

方法示例

让我们来编写一个简单的小程序,它会在结构体类型上创建一个方法并调用它。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type Employee struct {
  6. name string
  7. salary int
  8. currency string
  9. }
  10. /*
  11. displaySalary() 方法将 Employee 做为接收器类型
  12. */
  13. func (e Employee) displaySalary() {
  14. fmt.Printf("Salary of %s is %s%d", e.name, e.currency, e.salary)
  15. }
  16. func main() {
  17. emp1 := Employee {
  18. name: "Sam Adolf",
  19. salary: 5000,
  20. currency: "$",
  21. }
  22. emp1.displaySalary() // 调用 Employee 类型的 displaySalary() 方法
  23. }

在线运行程序

在上面程序的第 16 行,我们在 Employee 结构体类型上创建了一个 displaySalary 方法。displaySalary()方法在方法的内部访问了接收器 e Employee。在第 17 行,我们使用接收器 e,并打印 employee 的 name、currency 和 salary 这 3 个字段。

在第 26 行,我们调用了方法 emp1.displaySalary()

程序输出:Salary of Sam Adolf is $5000

为什么我们已经有函数了还需要方法呢?

上面的程序已经被重写为只使用函数,没有方法。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type Employee struct {
  6. name string
  7. salary int
  8. currency string
  9. }
  10. /*
  11. displaySalary()方法被转化为一个函数,把 Employee 当做参数传入。
  12. */
  13. func displaySalary(e Employee) {
  14. fmt.Printf("Salary of %s is %s%d", e.name, e.currency, e.salary)
  15. }
  16. func main() {
  17. emp1 := Employee{
  18. name: "Sam Adolf",
  19. salary: 5000,
  20. currency: "$",
  21. }
  22. displaySalary(emp1)
  23. }

在线运行程序

在上面的程序中,displaySalary 方法被转化为一个函数,Employee 结构体被当做参数传递给它。这个程序也产生完全相同的输出:Salary of Sam Adolf is $5000

既然我们可以使用函数写出相同的程序,那么为什么我们需要方法?这有着几个原因,让我们一个个的看看。

  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. type Rectangle struct {
  7. length int
  8. width int
  9. }
  10. type Circle struct {
  11. radius float64
  12. }
  13. func (r Rectangle) Area() int {
  14. return r.length * r.width
  15. }
  16. func (c Circle) Area() float64 {
  17. return math.Pi * c.radius * c.radius
  18. }
  19. func main() {
  20. r := Rectangle{
  21. length: 10,
  22. width: 5,
  23. }
  24. fmt.Printf("Area of rectangle %d\n", r.Area())
  25. c := Circle{
  26. radius: 12,
  27. }
  28. fmt.Printf("Area of circle %f", c.Area())
  29. }

在线运行程序

该程序输出:

  1. Area of rectangle 50
  2. Area of circle 452.389342

上面方法的属性被使用在接口中。我们将在接下来的教程中讨论这个问题。

指针接收器与值接收器

到目前为止,我们只看到了使用值接收器的方法。还可以创建使用指针接收器的方法。值接收器和指针接收器之间的区别在于,在指针接收器的方法内部的改变对于调用者是可见的,然而值接收器的情况不是这样的。让我们用下面的程序来帮助理解这一点。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type Employee struct {
  6. name string
  7. age int
  8. }
  9. /*
  10. 使用值接收器的方法。
  11. */
  12. func (e Employee) changeName(newName string) {
  13. e.name = newName
  14. }
  15. /*
  16. 使用指针接收器的方法。
  17. */
  18. func (e *Employee) changeAge(newAge int) {
  19. e.age = newAge
  20. }
  21. func main() {
  22. e := Employee{
  23. name: "Mark Andrew",
  24. age: 50,
  25. }
  26. fmt.Printf("Employee name before change: %s", e.name)
  27. e.changeName("Michael Andrew")
  28. fmt.Printf("\nEmployee name after change: %s", e.name)
  29. fmt.Printf("\n\nEmployee age before change: %d", e.age)
  30. (&e).changeAge(51)
  31. fmt.Printf("\nEmployee age after change: %d", e.age)
  32. }

在线运行程序

在上面的程序中,changeName 方法有一个值接收器 (e Employee),而 changeAge 方法有一个指针接收器 (e *Employee)。在 changeName 方法中对 Employee 结构体的字段 name 所做的改变对调用者是不可见的,因此程序在调用 e.changeName("Michael Andrew") 这个方法的前后打印出相同的名字。由于 changeAge 方法是使用指针 (e *Employee) 接收器的,所以在调用 (&e).changeAge(51) 方法对 age 字段做出的改变对调用者将是可见的。该程序输出如下:

  1. Employee name before change: Mark Andrew
  2. Employee name after change: Mark Andrew
  3. Employee age before change: 50
  4. Employee age after change: 51

在上面程序的第 36 行,我们使用 (&e).changeAge(51) 来调用 changeAge 方法。由于 changeAge 方法有一个指针接收器,所以我们使用 (&e) 来调用这个方法。其实没有这个必要,Go语言让我们可以直接使用 e.changeAge(51)e.changeAge(51) 会自动被Go语言解释为 (&e).changeAge(51)

下面的程序重写了,使用 e.changeAge(51) 来代替 (&e).changeAge(51),它输出相同的结果。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type Employee struct {
  6. name string
  7. age int
  8. }
  9. /*
  10. 使用值接收器的方法。
  11. */
  12. func (e Employee) changeName(newName string) {
  13. e.name = newName
  14. }
  15. /*
  16. 使用指针接收器的方法。
  17. */
  18. func (e *Employee) changeAge(newAge int) {
  19. e.age = newAge
  20. }
  21. func main() {
  22. e := Employee{
  23. name: "Mark Andrew",
  24. age: 50,
  25. }
  26. fmt.Printf("Employee name before change: %s", e.name)
  27. e.changeName("Michael Andrew")
  28. fmt.Printf("\nEmployee name after change: %s", e.name)
  29. fmt.Printf("\n\nEmployee age before change: %d", e.age)
  30. e.changeAge(51)
  31. fmt.Printf("\nEmployee age after change: %d", e.age)
  32. }

在线运行程序

那么什么时候使用指针接收器,什么时候使用值接收器?

一般来说,指针接收器可以使用在:对方法内部的接收器所做的改变应该对调用者可见时。

指针接收器也可以被使用在如下场景:当拷贝一个结构体的代价过于昂贵时。考虑下一个结构体有很多的字段。在方法内使用这个结构体做为值接收器需要拷贝整个结构体,这是很昂贵的。在这种情况下使用指针接收器,结构体不会被拷贝,只会传递一个指针到方法内部使用。

在其他的所有情况,值接收器都可以被使用。

匿名字段的方法

属于结构体的匿名字段的方法可以被直接调用,就好像这些方法是属于定义了匿名字段的结构体一样。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type address struct {
  6. city string
  7. state string
  8. }
  9. func (a address) fullAddress() {
  10. fmt.Printf("Full address: %s, %s", a.city, a.state)
  11. }
  12. type person struct {
  13. firstName string
  14. lastName string
  15. address
  16. }
  17. func main() {
  18. p := person{
  19. firstName: "Elon",
  20. lastName: "Musk",
  21. address: address {
  22. city: "Los Angeles",
  23. state: "California",
  24. },
  25. }
  26. p.fullAddress() //访问 address 结构体的 fullAddress 方法
  27. }

在线运行程序

在上面程序的第 32 行,我们通过使用 p.fullAddress() 来访问 address 结构体的 fullAddress() 方法。明确的调用 p.address.fullAddress() 是没有必要的。该程序输出:

  1. Full address: Los Angeles, California

在方法中使用值接收器 与 在函数中使用值参数

这个话题很多Go语言新手都弄不明白。我会尽量讲清楚。

当一个函数有一个值参数,它只能接受一个值参数。

当一个方法有一个值接收器,它可以接受值接收器和指针接收器。

让我们通过一个例子来理解这一点。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type rectangle struct {
  6. length int
  7. width int
  8. }
  9. func area(r rectangle) {
  10. fmt.Printf("Area Function result: %d\n", (r.length * r.width))
  11. }
  12. func (r rectangle) area() {
  13. fmt.Printf("Area Method result: %d\n", (r.length * r.width))
  14. }
  15. func main() {
  16. r := rectangle{
  17. length: 10,
  18. width: 5,
  19. }
  20. area(r)
  21. r.area()
  22. p := &r
  23. /*
  24. compilation error, cannot use p (type *rectangle) as type rectangle
  25. in argument to area
  26. */
  27. //area(p)
  28. p.area()//通过指针调用值接收器
  29. }

在线运行程序

第 12 行的函数 func area(r rectangle) 接受一个值参数,方法 func (r rectangle) area() 接受一个值接收器。

在第 25 行,我们通过值参数 area(r) 来调用 area 这个函数,这是合法的。同样,我们使用值接收器来调用 area 方法 r.area(),这也是合法的。

在第 28 行,我们创建了一个指向 r 的指针 p。如果我们试图把这个指针传递到只能接受一个值参数的函数 area,编译器将会报错。所以我把代码的第 33 行注释了。如果你把这行的代码注释去掉,编译器将会抛出错误 compilation error, cannot use p (type *rectangle) as type rectangle in argument to area.。这将会按预期抛出错误。

现在到了棘手的部分了,在第35行的代码 p.area() 使用指针接收器 p 调用了只接受一个值接收器的方法 area。这是完全有效的。原因是当 area 有一个值接收器时,为了方便Go语言把 p.area() 解释为 (*p).area()

该程序将会输出:

  1. Area Function result: 50
  2. Area Method result: 50
  3. Area Method result: 50

在方法中使用指针接收器 与 在函数中使用指针参数

和值参数相类似,函数使用指针参数只接受指针,而使用指针接收器的方法可以使用值接收器和指针接收器。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type rectangle struct {
  6. length int
  7. width int
  8. }
  9. func perimeter(r *rectangle) {
  10. fmt.Println("perimeter function output:", 2*(r.length+r.width))
  11. }
  12. func (r *rectangle) perimeter() {
  13. fmt.Println("perimeter method output:", 2*(r.length+r.width))
  14. }
  15. func main() {
  16. r := rectangle{
  17. length: 10,
  18. width: 5,
  19. }
  20. p := &r //pointer to r
  21. perimeter(p)
  22. p.perimeter()
  23. /*
  24. cannot use r (type rectangle) as type *rectangle in argument to perimeter
  25. */
  26. //perimeter(r)
  27. r.perimeter()//使用值来调用指针接收器
  28. }

在线运行程序

在上面程序的第 12 行,定义了一个接受指针参数的函数 perimeter。第 17 行定义了一个有一个指针接收器的方法。

在第 27 行,我们调用 perimeter 函数时传入了一个指针参数。在第 28 行,我们通过指针接收器调用了 perimeter 方法。所有一切看起来都这么完美。

在被注释掉的第 33 行,我们尝试通过传入值参数 r 调用函数 perimeter。这是不被允许的,因为函数的指针参数不接受值参数。如果你把这行的代码注释去掉并把程序运行起来,编译器将会抛出错误 main.go:33: cannot use r (type rectangle) as type *rectangle in argument to perimeter.

在第 35 行,我们通过值接收器 r 来调用有指针接收器的方法 perimeter。这是被允许的,为了方便Go语言把代码 r.perimeter() 解释为 (&r).perimeter()。该程序输出:

  1. perimeter function output: 30
  2. perimeter method output: 30
  3. perimeter method output: 30

在非结构体上的方法

到目前为止,我们只在结构体类型上定义方法。也可以在非结构体类型上定义方法,但是有一个问题。为了在一个类型上定义一个方法,方法的接收器类型定义和方法的定义应该在同一个包中。到目前为止,我们定义的所有结构体和结构体上的方法都是在同一个 main 包中,因此它们是可以运行的。

  1. package main
  2. func (a int) add(b int) {
  3. }
  4. func main() {
  5. }

在线运行程序

在上面程序的第 3 行,我们尝试把一个 add 方法添加到内置的类型 int。这是不允许的,因为 add 方法的定义和 int 类型的定义不在同一个包中。该程序会抛出编译错误 cannot define new methods on non-local type int

让该程序工作的方法是为内置类型 int 创建一个类型别名,然后创建一个以该类型别名为接收器的方法。

  1. package main
  2. import "fmt"
  3. type myInt int
  4. func (a myInt) add(b myInt) myInt {
  5. return a + b
  6. }
  7. func main() {
  8. num1 := myInt(5)
  9. num2 := myInt(10)
  10. sum := num1.add(num2)
  11. fmt.Println("Sum is", sum)
  12. }

在线运行程序

在上面程序的第5行,我们为 int 创建了一个类型别名 myInt。在第7行,我们定义了一个以 myInt 为接收器的的方法 add

该程序将会打印出 Sum is 15

我已经创建了一个程序,包含了我们迄今为止所讨论的所有概念,详见github

这就是Go中的方法。祝你有美好的一天。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注