method4.go 364 B

12345678910111213141516171819202122232425262728
  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. type Point struct {
  7. x, y float64
  8. }
  9. func (p *Point) Abs() float64 {
  10. return math.Sqrt(p.x*p.x + p.y*p.y)
  11. }
  12. type NamedPoint struct {
  13. Point
  14. name string
  15. }
  16. func (n *NamedPoint) Abs() float64 {
  17. return n.Point.Abs() * 100.
  18. }
  19. func main() {
  20. n := &NamedPoint{Point{3, 4}, "Pythagoras"}
  21. fmt.Println(n.Abs()) // prints 500
  22. }