Advertisement
cwchen

[Go] Embedding Point into Point3D

Oct 5th, 2017
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.00 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "log"
  5. )
  6.  
  7. type Point struct {
  8.     x float64
  9.     y float64
  10. }
  11.  
  12. func NewPoint(x float64, y float64) *Point {
  13.     p := new(Point)
  14.  
  15.     p.SetX(x)
  16.     p.SetY(y)
  17.  
  18.     return p
  19. }
  20.  
  21. func (p *Point) GetX() float64 {
  22.     return p.x
  23. }
  24.  
  25. func (p *Point) GetY() float64 {
  26.     return p.y
  27. }
  28.  
  29. func (p *Point) SetX(x float64) {
  30.     p.x = x
  31. }
  32.  
  33. func (p *Point) SetY(y float64) {
  34.     p.y = y
  35. }
  36.  
  37. type Point3D struct {
  38.     // Point is embedded
  39.     Point
  40.     z float64
  41. }
  42.  
  43. func NewPoint3D(x float64, y float64, z float64) *Point3D {
  44.     p := new(Point3D)
  45.  
  46.     p.SetX(x)
  47.     p.SetY(y)
  48.     p.SetZ(z)
  49.  
  50.     return p
  51. }
  52.  
  53. func (p *Point3D) GetZ() float64 {
  54.     return p.z
  55. }
  56.  
  57. func (p *Point3D) SetZ(z float64) {
  58.     p.z = z
  59. }
  60.  
  61. func main() {
  62.     p := NewPoint3D(1, 2, 3)
  63.  
  64.     // GetX method is from Point
  65.     if !(p.GetX() == 1) {
  66.         log.Fatal("Wrong value")
  67.     }
  68.  
  69.     // GetY method is from Point
  70.     if !(p.GetY() == 2) {
  71.         log.Fatal("Wrong value")
  72.     }
  73.  
  74.     // GetZ method is from Point3D
  75.     if !(p.GetZ() == 3) {
  76.         log.Fatal("Wrong value")
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement