開発環境
- OS: macOS High Sierra - Apple
- IDE(統合開発環境): Visual Studio for Mac
- プログラミング言語: C#
初めてのC# 第2版 (Jesse Liberty (著)、Brian MacDonald (著)、日向 俊二 (翻訳)、オライリージャパン)の11章(継承とポリモーフィズム)、11.9(練習問題)、問題11-2.を取り組んでみる。
コード
using System;
namespace Sample11_2
{
class Telephone
{
protected string phonetype;
public virtual void Ring()
{
Console.WriteLine("Ring the {0}", phonetype);
}
}
class ElectronicPhone : Telephone
{
public ElectronicPhone()
{
this.phonetype = "Digital";
}
public override void Ring()
{
base.Ring();
Console.Write("Pipi, Pipi.");
}
}
class Program
{
void Run()
{
Telephone t = new Telephone();
t.Ring();
ElectronicPhone ep = new ElectronicPhone();
ep.Ring();
}
static void Main(string[] args)
{
Program p = new Program();
p.Run();
}
}
}
入出力結果(Terminal)
Ring the Ring the Digital Pipi, Pipi. Press any key to continue...
0 コメント:
コメントを投稿