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) GetX() float64 {
- return p.x
- }
- func (p *Point) GetY() 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
- Point
- z float64
- }
- func NewPoint3D(x float64, y float64, z float64) *Point3D {
- p := new(Point3D)
- p.SetX(x)
- p.SetY(y)
- p.SetZ(z)
- return p
- }
- func (p *Point3D) GetZ() 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.GetX() == 1) {
- log.Fatal("Wrong value")
- }
- // GetY method is from Point
- if !(p.GetY() == 2) {
- log.Fatal("Wrong value")
- }
- // GetZ method is from Point3D
- if !(p.GetZ() == 3) {
- log.Fatal("Wrong value")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement