プログラミング(Python、Perl、C、Go、JavaScript)、数学、読書…
開発環境
"初めてのC# 第2版"のp.194, 第11章(継承とポリモーフィズム)の11.9(練習問題)、練習 11-1, 2, 3を解いてみる。
練習 11-1
練習 11-2
using System; public class Telephone { protected string phonetype; public virtual void Ring() { Console.WriteLine( "Ringing the {0}: Ring Ring, Ring Ring", phonetype); } } public class ElectronicPhone : Telephone { public ElectronicPhone() { this.phonetype = "Digital"; } public override void Ring() { Console.WriteLine( "Ringing the {0}: Pipipi, Pipipi", phonetype); } } public class Tester { public void Run() { ElectronicPhone ePhone = new ElectronicPhone(); ePhone.Ring(); } static void Main() { Tester t = new Tester(); t.Run(); } }
練習 11-3
using System; public abstract class Telephone { protected string phonetype; public abstract void Ring(); } public class ElectronicPhone : Telephone { public ElectronicPhone() { this.phonetype = "Digital"; } 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}: Talking", phonetype); } } public class Tester { public void Run() { Telephone ePhone = new ElectronicPhone(); Telephone tPhone = new TalkingPhone(); ePhone.Ring(); tPhone.Ring(); } static void Main() { Tester t = new Tester(); t.Run(); } }
0 コメント:
コメントを投稿