開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio Community 2017
初めてのC# 第2版 (Jesse Liberty (著)、Brian MacDonald (著)、日向 俊二 (翻訳)、オライリージャパン)の11章(継承とポリモーフィズム)、11.9(練習問題)、練習11-2.を取り組んでみる。
コード
using System; class Program { public void Run() { Telephone t = new Telephone(); ElectronicPhone ep = new ElectronicPhone(); t.Ring(); ep.Ring(); } static void Main(string[] args) { Program p = new Program(); p.Run(); } } class Telephone { protected string phonetype = "Telephone"; public virtual void Ring() { Console.WriteLine("Ring the " + phonetype); } } class ElectronicPhone : Telephone { public ElectronicPhone() { this.phonetype = "Digital"; } public override void Ring() { base.Ring(); Console.WriteLine("override"); } }
入出力結果(コマンドプロンプト)
Ring the Telephone Ring the Digital override 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿