【发布时间】:2016-11-23 16:47:30
【问题描述】:
v 是Vertex 的对象,Scale 是指向Vertex 的指针的方法。那么为什么v.Scale(10) 没有错,因为v 不是指向Vertex 对象的指针?谢谢。
package main
import (
"fmt"
"math"
)
type Vertex struct {
X, Y float64
}
func (v Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
func (v *Vertex) Scale(f float64) {
v.X = v.X * f
v.Y = v.Y * f
}
func main() {
v := Vertex{3, 4}
v.Scale(10)
fmt.Println(v.Abs())
}
【问题讨论】:
标签: function pointers go methods call