開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio Community 2017
Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の6章(インタフェースと抽象クラス - クラスに約束を守らせる)、プールパズル(p. 257)を取り組んでみる。
コード
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp18
{
public interface INose
{
string Face { get; }
int Ear();
}
public abstract class Picasso : INose
{
private string face;
public Picasso(string face)
{
this.face = face;
}
public virtual string Face
{
get { return face; }
}
public virtual int Ear()
{
return 7;
}
}
public class Clowns : Picasso
{
public Clowns() : base("Clowns") { }
}
public class Acts: Picasso
{
public Acts() : base("Acts") { }
public override int Ear()
{
return 5;
}
}
public class Of76: Clowns
{
public override string Face
{
get { return "Of76"; }
}
static void Main()
{
string result = "";
INose[] i = new INose[3];
i[0] = new Acts();
i[1] = new Clowns();
i[2] = new Of76();
for (int x = 0; x < 3; x++)
{
result += i[x].Ear() + " " + i[x].Face + "\n";
}
MessageBox.Show(result);
}
}
}
0 コメント:
コメントを投稿