| この問題を解くためには… |
| → 応用編第5日目参照 |
|---|
問題5:インターフェース
prob5-1.(難易度:★)
以下のプログラムが、期待された実行結果通りにプログラムが動くように、インターフェースICellPhone、IComputer、IMailerを実装しなさい。
プロジェクトProblemex5_1/Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problemex5_1
{
class Program
{
static void Main(string[] args)
{
CellPhone cp = new CellPhone();
FuncPhone(cp);
FuncMailer(cp);
FuncComputer(cp);
}
// 電話としての処理
public static void FuncPhone(IPhone phone)
{
phone.CallPhone(); // 電話を掛ける
phone.RecievePhone(); // 電話を受ける
}
// メーラーとしての処理
public static void FuncMailer(IMailer mailer)
{
mailer.SendMail(); // メールを送信する
mailer.RecieveMail(); // メールを受信する
}
// コンピュータとしての処理
public static void FuncComputer(IComputer computer)
{
computer.PlayGame(); // ゲームをする
computer.BrowseWeb(); // ウェブを閲覧する
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problemex5_1
{
class CellPhone : IComputer,IMailer,IPhone
{
// メールを送信する
public void SendMail(){
Console.WriteLine("メールを送る");
}
// メールを受信する
public void RecieveMail(){
Console.WriteLine("メールを受信する");
}
// webを閲覧する
public void BrowseWeb()
{
Console.WriteLine("ウェブを閲覧する");
}
// ゲームをする
public void PlayGame(){
Console.WriteLine("ゲームをする");
}
// 電話をかける
public void CallPhone(){
Console.WriteLine("電話を掛ける");
}
// 電話を受ける
public void RecievePhone(){
Console.WriteLine("電話を受ける");
}
}
}
電話を掛ける
電話を受ける
メールを送る
メールを受信する
ゲームをする
ウェブを閲覧する
電話を受ける
メールを送る
メールを受信する
ゲームをする
ウェブを閲覧する
prob5-2.(難易度:★)
以下のプログラムが、期待された実行結果通りにプログラムが動くように、インターフェースIAlarm、IClockを実装しなさい。
Problemex5_2/Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problemex5_2
{
class Program
{
static void Main(string[] args)
{
AlarmClock ac = new AlarmClock(); // アラーム付き時計クラスのインスタンスを生成
IAlarm ar = (IAlarm)ac;
IClock cl = (IClock)ac;
FuncAlarm(ar);
FuncClock(cl);
}
// アラームとしての処理
public static void FuncAlarm(IAlarm alarm)
{
alarm.SetAlarm(); // アラームのセット
alarm.Alarm(); // アラームを鳴らす
alarm.StopAlarm(); // アラームを止める
}
// 時計としての処理
public static void FuncClock(IClock clock)
{
clock.AdjustTime(); // 時間を調整する
clock.ShowTime(); // 時間を表示する
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problemex5_2
{
class AlarmClock: IAlarm,IClock
{
public void Alarm(){
Console.WriteLine("アラームを鳴らす");
}
public void SetAlarm(){
Console.WriteLine("アラームをセットする");
}
public void StopAlarm(){
Console.WriteLine("アラームを止める");
}
public void ShowTime(){
Console.WriteLine("時刻を知る");
}
public void AdjustTime(){
Console.WriteLine("時刻を修正する");
}
}
}
アラームをセットする
アラームを鳴らす
アラームを止める
時刻を修正する
時刻を知る
アラームを鳴らす
アラームを止める
時刻を修正する
時刻を知る









