プログラミング(Python、Perl、C、Go、JavaScript)、数学、読書…
開発環境
"初めてのC# 第2版"の第11章(継承とポリモーフィズム)の11.9(練習問題)を解いてみる。
練習 11-1
using System; public class Telephone { protected string phonetype; public void Ring() { Console.WriteLine("Ringing the {0}", phonetype); } } public class ElectronicPhone : Telephone { public ElectronicPhone() { this.phonetype = "Degital"; } } class Tester { public void Run() { ElectronicPhone phone = new ElectronicPhone(); phone.Ring(); } static void Main() { Tester t = new Tester(); t.Run(); } }
出力結果
Ringing the Degital 続行するには何かキーを押してください . . .
練習 11-2
using System; public class Telephone { protected string phonetype; public virtual void Ring() { Console.WriteLine("Ringing the {0}", phonetype); } } public class ElectronicPhone : Telephone { public ElectronicPhone() { this.phonetype = "Degital"; } public override void Ring() { Console.WriteLine("Ringing the {0}. Pipipi, Pipipi.", phonetype); } } class Tester { public void Run() { ElectronicPhone phone = new ElectronicPhone(); phone.Ring(); } static void Main() { Tester t = new Tester(); t.Run(); } }
Ringing the Degital. Pipipi, Pipipi. 続行するには何かキーを押してください . . .
練習 11-3
using System; public abstract class Telephone { protected string phonetype; public abstract void Ring(); } public class ElectronicPhone : Telephone { public ElectronicPhone() { this.phonetype = "Degital"; } public override void Ring() { Console.WriteLine("Ringing the {0}. Pipipi, Pipipi.", phonetype); } } public class TalkingPhone : Telephone { public TalkingPhone() { this.phonetype = "Talking"; } public override void Ring() { Console.WriteLine("Ringing the {0}. Hi!", phonetype); } } class Tester { public void Run() { ElectronicPhone ep = new ElectronicPhone(); TalkingPhone tp = new TalkingPhone(); ep.Ring(); tp.Ring(); } static void Main() { Tester t = new Tester(); t.Run(); } }
Ringing the Degital. Pipipi, Pipipi. Ringing the Talking. Hi! 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿