プログラミング(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 = "Digital"; } } class Tester { public void Run() { ElectronicPhone phone = new ElectronicPhone(); phone.Ring(); } static void Main() { Tester t = new Tester(); t.Run(); } }
出力結果
Ringing the Digital 続行するには何かキーを押してください . . .
練習 11-2
using System; public class Telephone { protected string phonetype; public Telephone() { this.phonetype = "Phone"; } public virtual void Ring() { Console.WriteLine( "Ringing the {0}. Ring, Ring." , phonetype); } } public class ElectronicPhone : Telephone { public ElectronicPhone() { this.phonetype = "Digital"; } public override void Ring() { Console.WriteLine( "Ringing the {0}. Pipi, Pipi." , phonetype); } } class Tester { public void Run() { Telephone phone = new Telephone(); ElectronicPhone ePhone = new ElectronicPhone(); phone.Ring(); ePhone.Ring(); } static void Main() { Tester t = new Tester(); t.Run(); } }
Ringing the Phone. Ring, Ring. Ringing the Digital. Pipi, Pipi. 続行するには何かキーを押してください . . .
練習 11-3
using System; abstract public class Telephone { protected string phonetype; public abstract void Ring(); } public class DigitalPhone : Telephone { public DigitalPhone() { this.phonetype = "Digital"; } public override void Ring() { Console.WriteLine( "Ringing the {0}. Pipi, Pipi.", phonetype); } } public class TalkingPhone : Telephone { public TalkingPhone() { this.phonetype = "Talking"; } public override void Ring() { Console.WriteLine( "Ringing the {0}. Ring, Ring.", phonetype); } } class Tester { public void Run() { DigitalPhone dPhone = new DigitalPhone(); TalkingPhone tPhone = new TalkingPhone(); dPhone.Ring(); tPhone.Ring(); } static void Main() { Tester t = new Tester(); t.Run(); } }
Ringing the Digital. Pipi, Pipi. Ringing the Talking. Ring, Ring. 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿