| この問題を解くためには… |
| → 応用編第4日目参照 |
|---|
問題4:抽象クラス
probex4-1.(難易度:★)
以下のプログラムは、抽象クラスAnimal(動物)クラスと、それを継承した、Dog(犬)クラス、およびMonkey(猿)クラスを生成し、その各メソッドを呼び出したものである。ただ、Monkeyクラスのソースコードが存在しない。そこで、期待されたる実行結果とDogクラスを参考にしながら、Monkeyクラスを実装し、プログラムを完成させなさい。
プロジェクトProblemex4_1/Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problemex4_1
{
class Program
{
static void Main(string[] args)
{
// Animalクラスのオブジェクトの配列を生成
Animal[] animals = new Animal[2];
animals[0] = new Dog(); // 犬クラスのインスタンス生成
animals[1] = new Monkey(); // 猿クラスのインスタンスの生成
for (int i = 0; i < animals.Length; i++)
{
animals[i].ShowName();
animals[i].Move();
animals[i].Bark();
Console.WriteLine("------------");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problemex4_1
{
// 抽象クラス動物
abstract class Animal
{
// 名前
protected string name = "";
// 吠える
public abstract void Bark();
// 動く
public abstract void Move();
// 名前を表示する
public void ShowName()
{
Console.WriteLine("名前:{0}", name);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problemex4_1
{
// 犬クラス
class Dog : Animal
{
public Dog()
{
name = "犬";
}
// 吠える
public override void Bark()
{
Console.WriteLine("ワンワン");
}
// 動く
public override void Move()
{
Console.WriteLine("歩く");
}
}
}
名前:犬
歩く
ワンワン
------------
名前:猿
木に登る
キーキー
------------
歩く
ワンワン
------------
名前:猿
木に登る
キーキー
------------
probex4-2.(難易度:★)
以下のプログラムは、二種類の図形、四角形(Box)と、三角形(Triangle)クラスのインスタンスを生成し、各クラスのメソッドを実行するプログラムである。この2つのクラスには、共通するメンバが存在する。
そこで、これら2つのクラスに共通するメンバを、平面図形(PlaneFigure)クラスに集約し、Boxクラス、およびTriangleクラスは、それらのクラスを継承したものにするように、プログラムを書きかかえなさい。ただし、この時、PlaneFigureクラスは、抽象プロパティAreaを持つ抽象クラスとすること。
プロジェクトProblemex4_2/Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problemex4_2
{
class Program
{
static void Main(string[] args)
{
// 四角形の生成
Box b = new Box(2.0,4.0);
// 三角形の生成
Triangle t = new Triangle(4.0, 1.5);
Console.WriteLine("幅{0}、高さ{1}の四角形の面積は{2}", b.Width, b.Height, b.Area);
Console.WriteLine("幅{0}、高さ{1}の三角形の面積は{2}", t.Width, t.Height, t.Area);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problemex4_2
{
// 四角形クラス
class Box
{
// 面積
private double area = 0;
// 幅
private double width = 0;
// 高さ
private double height = 0;
// コンストラクタ(引数なし)
public Box()
{
}
// コンストラクタ(引数あり)
public Box(double width, double height)
{
this.width = width;
this.height = height;
}
// 幅のプロパティ
public double Width
{
get { return width; }
set { width = value; }
}
// 高さのプロパティ
public double Height
{
get { return height; }
set { height = value; }
}
// 面積の取得
public double Area
{
get { return width * height; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problemex4_2
{
// 三角形クラス
class Triangle
{
// 面積
private double area = 0;
// 底辺
private double width = 0;
// 高さ
private double height = 0;
// コンストラクタ(引数なし)
public Triangle()
{
}
// コンストラクタ(引数あり)
public Triangle(double width, double height)
{
this.width = width;
this.height = height;
}
// 幅のプロパティ
public double Width
{
get { return width; }
set { width = value; }
}
// 高さのプロパティ
public double Height
{
get { return height; }
set { height = value; }
}
// 面積の取得
public double Area
{
get { return width * height / 2.0; }
}
}
}
幅2、高さ4の四角形の面積は8
幅4、高さ1.5の三角形の面積は3
幅4、高さ1.5の三角形の面積は3









