LoginSignup
0
0

More than 3 years have passed since last update.

go修行10日目 embeded

Posted at

embeded

  • Go言語には継承がなくてその代わり
  • VertexをVertex3Dがembededしている


package main

import "fmt"

type Vertex struct{
    x, y int
}

func (v Vertex) Area() int{
    return v.x * v.y
}

func (v *Vertex) Scale(i int){
    v.x = v.x * i
    v.y = v.y * i
}

type Vertex3D struct{
    Vertex
    z int
}

func (v Vertex3D) Area3D() int{
    return v.x * v.y * v.z
}

func (v *Vertex3D) Scale3D(i int){
    v.x = v.x * i
    v.y = v.y * i
    v.z = v.z * i
}

func New(x, y, z int) *Vertex3D{
    return &Vertex3D{Vertex{x, y}, z}
}

func main() {
    v := New(3,4,5)
    v.Scale(10)
    fmt.Println(v.Area())
    fmt.Println(v.Area3D())
}
1200
6000
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0