株式会社ヴァンデミックシステム

Blog

<スポンサーリンク>

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

<スポンサーリンク>

コメントを残す

Allowed tags:  you may use these HTML tags and attributes: <a href="">, <strong>, <em>, <h1>, <h2>, <h3>
Please note:  all comments go through moderation.

*

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)