Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "log"
- )
- type Point struct {
- x float64
- y float64
- }
- func NewPoint(x float64, y float64) *Point {
- p := new(Point)
- p.SetX(x)
- p.SetY(y)
- return p
- }
- func (p *Point) X() float64 {
- return p.x
- }
- func (p *Point) Y() float64 {
- return p.y
- }
- func (p *Point) SetX(x float64) {
- p.x = x
- }
- func (p *Point) SetY(y float64) {
- p.y = y
- }
- type Point3D struct {
- // Point is embedded as a pointer
- *Point
- z float64
- }
- func NewPoint3D(x float64, y float64, z float64) *Point3D {
- p := new(Point3D)
- // Forward promotion
- p.Point = NewPoint(x, y)
- // Forward promotion
- p.Point.SetX(x)
- p.Point.SetY(y)
- p.SetZ(z)
- return p
- }
- func (p *Point3D) Z() float64 {
- return p.z
- }
- func (p *Point3D) SetZ(z float64) {
- p.z = z
- }
- func main() {
- p := NewPoint3D(1, 2, 3)
- // GetX method is from Point
- if !(p.X() == 1) {
- log.Fatal("Wrong value")
- }
- // GetY method is from Point
- if !(p.Y() == 2) {
- log.Fatal("Wrong value")
- }
- // GetZ method is from Point3D
- if !(p.Z() == 3) {
- log.Fatal("Wrong value")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement