Head First C#
頭とからだで覚えるC#の基本
(オライリージャパン)
Andrew Stellman (著), Jennifer Green (著)
佐藤 嘉一 (監修), 木下 哲也 (翻訳)
開発環境
- Microsoft Windows 8.1 Pro (VMware Fusion 6, OS X Mavericks - Apple) (OS)
- C# (プログラミング言語)
- Microsoft Visual Studio Express 2013 for Windows Desktop (統合開発環境, IDE)
Head First C#―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Green (著)、佐藤 嘉一 (監修)、木下 哲也 (翻訳)、オライリージャパン)の6章(インタフェースと抽象クラス: クラスに約束を守らせる)、エクササイズ(p.241)を解いてみる。
エクササイズ(p.241)
コード
Form1.cs
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ScaryScary fingersTheClown = new ScaryScary("大きな靴", 32);
FunnyFunny someFunnyClown = fingersTheClown;
IScaryClown someOtherScaryClown = someFunnyClown as IScaryClown;
someOtherScaryClown.Honk(); // プップー!私は大きな靴
}
}
interface IClown
{
string FunnyThingIHave { get; }
void Honk();
}
interface IScaryClown : IClown
{
string ScaryThingIHave { get; }
void ScareLittleChildren();
}
class FunnyFunny : IClown
{
private string funnyThingIHave;
public FunnyFunny(string funnyThingIHave)
{
this.funnyThingIHave = funnyThingIHave;
}
public string FunnyThingIHave
{
get { return funnyThingIHave; }
}
public void Honk()
{
MessageBox.Show("プップー!私は" + funnyThingIHave);
}
}
class ScaryScary : FunnyFunny, IScaryClown
{
private int numberOfScaryThings;
public ScaryScary(string funnyThingIHave, int numberOfScaryThings)
: base(funnyThingIHave)
{
this.numberOfScaryThings = numberOfScaryThings;
}
public string ScaryThingIHave
{
get { return numberOfScaryThings + "匹のクモをもっています"; }
}
public void ScareLittleChildren()
{
MessageBox.Show("ブー!残念!");
}
}
}
0 コメント:
コメントを投稿