開発環境
- Microsoft Windows 7 Home Premium (OS)
- Microsoft Visual C# 2010 Express Edition (IDE)
- 言語: C#
『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487311-294-7)の 第11章(継承とポリモーフィズム)11.9(練習問題)練習10-3を解いてみる。
その他参考書籍
練習10-3.
コード
using System;
abstract class Telephone
{
protected string phonetype;
public Telephone()
{
this.phonetype = "Telephone";
}
public abstract void Ring();
}
class DigitalPhone : Telephone
{
public DigitalPhone()
{
this.phonetype = "DigitalPhone";
}
public override void Ring()
{
Console.WriteLine("Ringing the {0}", phonetype);
Console.WriteLine("Sound: Pipi, Pipi");
}
}
class TalkingPhone : Telephone
{
public TalkingPhone()
{
this.phonetype = "TalkingPhone";
}
public override void Ring()
{
Console.WriteLine("Ringing the {0}", phonetype);
Console.WriteLine("Sound: Pipi, Pipi");
}
}
class Tester
{
public void Run()
{
DigitalPhone dp = new DigitalPhone();
TalkingPhone tp = new TalkingPhone();
Telephone[] ts = { dp, tp };
foreach (Telephone phone in ts)
{
phone.Ring();
}
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
入出力結果(Console Window)
Ringing the DigitalPhone Sound: Pipi, Pipi Ringing the TalkingPhone Sound: Pipi, Pipi 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿