使用接口作为字段的GO语言

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

GO using interfaces as fields

问题

我正在学习《Go语言编程入门》并试图理解接口。我觉得我对接口的概念和为什么需要它们有一个大致的了解,但是我在使用它们时遇到了困难。在这一节的结尾,他们有以下内容:

接口也可以用作字段:

  1. type MultiShape struct {
  2. shapes []Shape
  3. }

我们甚至可以通过给它一个area方法将MultiShape本身变成一个Shape:

  1. func (m *MultiShape) area() float64 {
  2. var area float64
  3. for _, s := range m.shapes {
  4. area += s.area()
  5. }
  6. return area
  7. }

现在,MultiShape可以包含Circles、Rectangles甚至其他的MultiShapes。

我不知道如何使用这个。我理解的是MultiShape可以在它的slice中包含CircleRectangle

这是我正在使用的示例代码:

  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. type Shape interface {
  7. area() float64
  8. }
  9. type MultiShape struct {
  10. shapes []Shape
  11. }
  12. func (m *MultiShape) area() float64 {
  13. var area float64
  14. for _, s := range m.shapes {
  15. area += s.area()
  16. }
  17. return area
  18. }
  19. // ===============================================
  20. // Rectangles
  21. type Rectangle struct {
  22. x1, y1, x2, y2 float64
  23. }
  24. func distance(x1, y1, x2, y2 float64) float64 {
  25. a := x2 - x1
  26. b := y2 - y1
  27. return math.Sqrt(a*a + b*b)
  28. }
  29. func (r *Rectangle) area() float64 {
  30. l := distance(r.x1, r.y1, r.x1, r.y2)
  31. w := distance(r.x1, r.y1, r.x2, r.y1)
  32. return l * w
  33. }
  34. // ===============================================
  35. // Circles
  36. type Circle struct {
  37. x, y, r float64
  38. }
  39. func (c *Circle) area() float64 {
  40. return math.Pi * c.r * c.r
  41. }
  42. // ===============================================
  43. func totalArea(shapes ...Shape) float64 {
  44. var area float64
  45. for _, s := range shapes {
  46. area += s.area()
  47. }
  48. return area
  49. }
  50. func main() {
  51. c := Circle{0, 0, 5}
  52. fmt.Println(c.area())
  53. r := Rectangle{0, 0, 10, 10}
  54. fmt.Println(r.area())
  55. fmt.Println(totalArea(&r, &c))
  56. //~ 这个不起作用,但这是我对它的理解
  57. //~ m := []MultiShape{c, r}
  58. //~ fmt.Println(totalArea(&m))
  59. }

有人可以帮助我吗?我有Python的背景,如果两者之间有某种联系,那将会有所帮助。

谢谢

英文:

I'm going through An Introduction to Programming in Go and trying to grasp interfaces. I feel like I have an ok idea of what they are and why we would need them but I'm having trouble using them. At the end of the section they have

>Interfaces can also be used as fields:
>
type MultiShape struct {
shapes []Shape
}
>We can even turn MultiShape itself into a Shape by giving it an area method:
>
func (m *MultiShape) area() float64 {
var area float64
for _, s := range m.shapes {
area += s.area()
}
return area
}
>Now a MultiShape can contain Circles, Rectangles or even other MultiShapes.

I don't know how to use this. My understanding of this is MultiShape can have a Circle and Rectangle in it's slice

This is the example code I'm working with

  1. package main
  2. import ("fmt"; "math")
  3. type Shape interface {
  4. area() float64
  5. }
  6. type MultiShape struct {
  7. shapes []Shape
  8. }
  9. func (m *MultiShape) area() float64 {
  10. var area float64
  11. for _, s := range m.shapes {
  12. area += s.area()
  13. }
  14. return area
  15. }
  16. // ===============================================
  17. // Rectangles
  18. type Rectangle struct {
  19. x1, y1, x2, y2 float64
  20. }
  21. func distance(x1, y1, x2, y2 float64) float64 {
  22. a := x2 - x1
  23. b := y2 - y1
  24. return math.Sqrt(a*a + b*b)
  25. }
  26. func (r *Rectangle) area() float64 {
  27. l := distance(r.x1, r.y1, r.x1, r.y2)
  28. w := distance(r.x1, r.y1, r.x2, r.y1)
  29. return l*w
  30. }
  31. // ===============================================
  32. // Circles
  33. type Circle struct {
  34. x, y, r float64
  35. }
  36. func (c * Circle) area() float64 {
  37. return math.Pi * c.r*c.r
  38. }
  39. // ===============================================
  40. func totalArea(shapes ...Shape) float64 {
  41. var area float64
  42. for _, s := range shapes {
  43. area += s.area()
  44. }
  45. return area
  46. }
  47. func main() {
  48. c := Circle{0,0,5}
  49. fmt.Println(c.area())
  50. r := Rectangle{0, 0, 10, 10}
  51. fmt.Println(r.area())
  52. fmt.Println(totalArea(&r, &c))
  53. //~ This doesn't work but this is my understanding of it
  54. //~ m := []MultiShape{c, r}
  55. //~ fmt.Println(totalArea(&m))
  56. }

Can someone help me with this? I have a python background so if there is some kind of link between the two that would help.

Thanks

答案1

得分: 3

例如,

  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. type Shape interface {
  7. area() float64
  8. }
  9. type MultiShape struct {
  10. shapes []Shape
  11. }
  12. func (m *MultiShape) area() float64 {
  13. var area float64
  14. for _, s := range m.shapes {
  15. area += s.area()
  16. }
  17. return area
  18. }
  19. type Rectangle struct {
  20. x1, y1, x2, y2 float64
  21. }
  22. func (s *Rectangle) area() float64 {
  23. x := math.Abs(s.x2 - s.x1)
  24. y := math.Abs(s.y2 - s.y1)
  25. return x * y
  26. }
  27. func (s *Rectangle) distance() float64 {
  28. x := s.x2 - s.x1
  29. y := s.y2 - s.y1
  30. return math.Sqrt(x*x + y*y)
  31. }
  32. type Circle struct {
  33. x, y, r float64
  34. }
  35. func (s *Circle) area() float64 {
  36. return math.Pi * s.r * s.r
  37. }
  38. func main() {
  39. r1 := Rectangle{1, 1, 4, 4}
  40. fmt.Println(r1.area())
  41. r2 := Rectangle{2, 4, 3, 6}
  42. fmt.Println(r2.area())
  43. c := Circle{10, 10, 2}
  44. fmt.Println(c.area())
  45. m := MultiShape{[]Shape{&r1, &r2, &c}}
  46. fmt.Println(m.area())
  47. }

输出:

  1. 9
  2. 2
  3. 12.566370614359172
  4. 23.566370614359172
英文:

For example,

  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. type Shape interface {
  7. area() float64
  8. }
  9. type MultiShape struct {
  10. shapes []Shape
  11. }
  12. func (m *MultiShape) area() float64 {
  13. var area float64
  14. for _, s := range m.shapes {
  15. area += s.area()
  16. }
  17. return area
  18. }
  19. type Rectangle struct {
  20. x1, y1, x2, y2 float64
  21. }
  22. func (s *Rectangle) area() float64 {
  23. x := math.Abs(s.x2 - s.x1)
  24. y := math.Abs(s.y2 - s.y1)
  25. return x * y
  26. }
  27. func (s *Rectangle) distance() float64 {
  28. x := s.x2 - s.x1
  29. y := s.y2 - s.y1
  30. return math.Sqrt(x*x + y*y)
  31. }
  32. type Circle struct {
  33. x, y, r float64
  34. }
  35. func (s *Circle) area() float64 {
  36. return math.Pi * s.r * s.r
  37. }
  38. func main() {
  39. r1 := Rectangle{1, 1, 4, 4}
  40. fmt.Println(r1.area())
  41. r2 := Rectangle{2, 4, 3, 6}
  42. fmt.Println(r2.area())
  43. c := Circle{10, 10, 2}
  44. fmt.Println(c.area())
  45. m := MultiShape{[]Shape{&r1, &r2, &c}}
  46. fmt.Println(m.area())
  47. }

Output:

  1. 9
  2. 2
  3. 12.566370614359172
  4. 23.566370614359172

huangapple
  • 本文由 发表于 2013年7月3日 08:31:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/17437918.html
匿名

发表评论

匿名网友

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

确定