blog/content/post/2008/05/21/2008-05-21-00000929.md

154 lines
6.3 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: ポリモーフィズムの練習
author: kazu634
date: 2008-05-21
url: /2008/05/21/_1001/
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4033;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- java
- Programming
---
<div class="section">
<p>
 とりあえず研修ではオブジェクト指向の考え方まで学び終わったところです。やってみると以外に理解できたというのが感想。ポリモーフィズムの練習に作ったソースを貼り付けておきますね。
</p>
<ul>
<li>
Test.java
</li>
<li>
Shape.java (&#8211;> インターフェース) <ul>
<li>
Circle.java
</li>
<li>
Square.java
</li>
<li>
Triangle.java
</li>
<li>
Trapezium.java
</li>
</ul>
</li>
</ul>
<pre class="syntax-highlight">
<span class="synType">public</span> <span class="synType">class</span> Test
{
<span class="synType">public</span> <span class="synType">static</span> <span class="synType">void</span> main(String args[])
{
Shape shp[] = <span class="synStatement">new</span> Shape[<span class="synConstant">4</span>];
<span class="synType">double</span> sum = <span class="synConstant"></span>;
shp[<span class="synConstant"></span>] = <span class="synStatement">new</span> Circle(<span class="synConstant">5</span>);
shp[<span class="synConstant">1</span>] = <span class="synStatement">new</span> Square(<span class="synConstant">10</span>);
shp[<span class="synConstant">2</span>] = <span class="synStatement">new</span> Trapezium(<span class="synConstant">3</span>, <span class="synConstant">5</span>, <span class="synConstant">5</span>);
shp[<span class="synConstant">3</span>] = <span class="synStatement">new</span> Triangle(<span class="synConstant">2</span>, <span class="synConstant">2.5</span>);
<span class="synStatement">for</span> (<span class="synType">int</span> i = <span class="synConstant"></span>;i &#60; shp.length;i++) {
shp[i].show();
sum += shp[i].getArea();
}
System.out.println(<span class="synConstant">&#34;=========&#34;</span>);
System.out.println(<span class="synConstant">&#34;面積の合計: &#34;</span> + sum);
}
}
</pre>
<pre class="syntax-highlight">
<span class="synType">public</span> <span class="synType">interface</span> Shape
{
<span class="synType">public</span> <span class="synType">static</span> <span class="synType">final</span> <span class="synType">double</span> PI = <span class="synConstant">3.14</span>;
<span class="synType">public</span> <span class="synType">abstract</span> <span class="synType">double</span> getArea();
<span class="synType">public</span> <span class="synType">abstract</span> <span class="synType">void</span> show();
}
</pre>
<pre class="syntax-highlight">
<span class="synType">public</span> <span class="synType">class</span> Circle <span class="synType">implements</span> Shape
{
<span class="synType">private</span> <span class="synType">double</span> radium;
<span class="synType">public</span> Circle(<span class="synType">double</span> r)
{
<span class="synType">this</span>.radium = r;
}
<span class="synType">public</span> <span class="synType">double</span> getArea()
{
<span class="synStatement">return</span>(radium * radium * PI);
}
<span class="synType">public</span> <span class="synType">void</span> show()
{
System.out.println (<span class="synConstant">&#34;円の面積: &#34;</span> + getArea());
}
}
</pre>
<pre class="syntax-highlight">
<span class="synType">public</span> <span class="synType">class</span> Square <span class="synType">implements</span> Shape
{
<span class="synType">private</span> <span class="synType">double</span> side;
<span class="synType">public</span> Square(<span class="synType">double</span> side)
{
<span class="synType">this</span>.side = side;
}
<span class="synType">public</span> <span class="synType">double</span> getArea()
{
<span class="synStatement">return</span>(side * side);
}
<span class="synType">public</span> <span class="synType">void</span> show()
{
System.out.println (<span class="synConstant">&#34;正方形の面積: &#34;</span> + getArea());
}
}
</pre>
<pre class="syntax-highlight">
<span class="synType">public</span> <span class="synType">class</span> Triangle <span class="synType">implements</span> Shape
{
<span class="synType">private</span> <span class="synType">double</span> side;
<span class="synType">private</span> <span class="synType">double</span> height;
<span class="synType">public</span> Triangle(<span class="synType">double</span> side, <span class="synType">double</span> height)
{
<span class="synType">this</span>.side = side;
<span class="synType">this</span>.height = height;
}
<span class="synType">public</span> <span class="synType">double</span> getArea()
{
<span class="synStatement">return</span>(side * height / <span class="synConstant">2</span>);
}
<span class="synType">public</span> <span class="synType">void</span> show()
{
System.out.println (<span class="synConstant">&#34;三角形の面積: &#34;</span> + getArea());
}
}
</pre>
<pre class="syntax-highlight">
<span class="synType">class</span> Trapezium <span class="synType">implements</span> Shape
{
<span class="synType">private</span> <span class="synType">double</span> upper;
<span class="synType">private</span> <span class="synType">double</span> lower;
<span class="synType">private</span> <span class="synType">double</span> height;
<span class="synType">public</span> Trapezium(<span class="synType">double</span> upper, <span class="synType">double</span> lower, <span class="synType">double</span> height)
{
<span class="synType">this</span>.upper = upper;
<span class="synType">this</span>.lower = lower;
<span class="synType">this</span>.height = height;
}
<span class="synType">public</span> <span class="synType">double</span> getArea()
{
<span class="synStatement">return</span> (upper + lower) * height / <span class="synConstant">2</span>;
}
<span class="synType">public</span> <span class="synType">void</span> show()
{
System.out.println (<span class="synConstant">&#34;台形の面積: &#34;</span> + getArea());
}
}
</pre>
<p>
</p>
</div>