embeded
- Go言語には継承がなくてその代わり
- VertexをVertex3Dがembededしている
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="s">"fmt"</span> <span class="k">type</span> <span class="n">Vertex</span> <span class="k">struct</span><span class="p">{</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="kt">int</span> <span class="p">}</span> <span class="k">func</span> <span class="p">(</span><span class="n">v</span> <span class="n">Vertex</span><span class="p">)</span> <span class="n">Area</span><span class="p">()</span> <span class="kt">int</span><span class="p">{</span> <span class="k">return</span> <span class="n">v</span><span class="o">.</span><span class="n">x</span> <span class="o">*</span> <span class="n">v</span><span class="o">.</span><span class="n">y</span> <span class="p">}</span> <span class="k">func</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span><span class="n">Vertex</span><span class="p">)</span> <span class="n">Scale</span><span class="p">(</span><span class="n">i</span> <span class="kt">int</span><span class="p">){</span> <span class="n">v</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">v</span><span class="o">.</span><span class="n">x</span> <span class="o">*</span> <span class="n">i</span> <span class="n">v</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">v</span><span class="o">.</span><span class="n">y</span> <span class="o">*</span> <span class="n">i</span> <span class="p">}</span> <span class="k">type</span> <span class="n">Vertex3D</span> <span class="k">struct</span><span class="p">{</span> <span class="n">Vertex</span> <span class="n">z</span> <span class="kt">int</span> <span class="p">}</span> <span class="k">func</span> <span class="p">(</span><span class="n">v</span> <span class="n">Vertex3D</span><span class="p">)</span> <span class="n">Area3D</span><span class="p">()</span> <span class="kt">int</span><span class="p">{</span> <span class="k">return</span> <span class="n">v</span><span class="o">.</span><span class="n">x</span> <span class="o">*</span> <span class="n">v</span><span class="o">.</span><span class="n">y</span> <span class="o">*</span> <span class="n">v</span><span class="o">.</span><span class="n">z</span> <span class="p">}</span> <span class="k">func</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span><span class="n">Vertex3D</span><span class="p">)</span> <span class="n">Scale3D</span><span class="p">(</span><span class="n">i</span> <span class="kt">int</span><span class="p">){</span> <span class="n">v</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">v</span><span class="o">.</span><span class="n">x</span> <span class="o">*</span> <span class="n">i</span> <span class="n">v</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">v</span><span class="o">.</span><span class="n">y</span> <span class="o">*</span> <span class="n">i</span> <span class="n">v</span><span class="o">.</span><span class="n">z</span> <span class="o">=</span> <span class="n">v</span><span class="o">.</span><span class="n">z</span> <span class="o">*</span> <span class="n">i</span> <span class="p">}</span> <span class="k">func</span> <span class="n">New</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="kt">int</span><span class="p">)</span> <span class="o">*</span><span class="n">Vertex3D</span><span class="p">{</span> <span class="k">return</span> <span class="o">&</span><span class="n">Vertex3D</span><span class="p">{</span><span class="n">Vertex</span><span class="p">{</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">},</span> <span class="n">z</span><span class="p">}</span> <span class="p">}</span> <span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">v</span> <span class="o">:=</span> <span class="n">New</span><span class="p">(</span><span class="m">3</span><span class="p">,</span><span class="m">4</span><span class="p">,</span><span class="m">5</span><span class="p">)</span> <span class="n">v</span><span class="o">.</span><span class="n">Scale</span><span class="p">(</span><span class="m">10</span><span class="p">)</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">v</span><span class="o">.</span><span class="n">Area</span><span class="p">())</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">v</span><span class="o">.</span><span class="n">Area3D</span><span class="p">())</span> <span class="p">}</span> |
1 2 |
1200 6000 |