2018年4月15日日曜日

開発環境

Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の6章(インタフェースと抽象クラス - クラスに約束を守らせる)、長いエクササイズ(p. 260)を取り組んでみる。

コード

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp19
{
    public partial class Form1 : Form
    {
        Location currentLocation;
        RoomWithDoor livingRoom;

        public Form1()
        {
            InitializeComponent();

            CreateObjects();
            MoveToANewLocation(livingRoom);
        }

        private void CreateObjects()
        {
            livingRoom = new RoomWithDoor("リビングルーム",
                "アンティークカーペット",
                "真ちゅうのノブを持つオーク材のドア");
            Room diningRoom = new Room("ダイニングルーム", "クリスタルのシャンデリア");
            RoomWithDoor kitchen = new RoomWithDoor("台所", "ステンレス製の電化製品", "網戸");
            OutsideWithDoor frontYard = new OutsideWithDoor("前庭", false, 
                "真ちゅうのノブを持つオーク材のドア");
            Outside garden = new Outside("庭園", false);
            OutsideWithDoor backYard = new OutsideWithDoor("裏庭", true, "網戸");

            livingRoom.exits = new Location[] { diningRoom };
            diningRoom.exits = new Location[] { livingRoom, kitchen };
            kitchen.exits = new Location[] { diningRoom };
            frontYard.exits = new Location[] { backYard, garden };
            garden.exits = new Location[] { frontYard, backYard };
            backYard.exits = new Location[] { frontYard, garden };

            livingRoom.DoorLocation = frontYard;
            frontYard.DoorLocation = livingRoom;
            kitchen.DoorLocation = backYard;
            backYard.DoorLocation = kitchen;
        }

        private void MoveToANewLocation(Location loc)
        {
            currentLocation = loc;
            exits.Items.Clear();
            foreach (Location item in loc.exits)
            {
                exits.Items.Add(item.Name);
            }
            exits.SelectedIndex = 0;
            description.Text = loc.Description;
            if (loc is IHasExteriorDoor)
            {
                goThroughTheDoor.Visible = true;
            }
            else
            {
                goThroughTheDoor.Visible = false;
            }

        }

        private void goHere_Click(object sender, EventArgs e)
        {
            MoveToANewLocation(currentLocation.exits[exits.SelectedIndex]);
        }

        private void goThroughTheDoor_Click(object sender, EventArgs e)
        {
            IHasExteriorDoor door = currentLocation as IHasExteriorDoor;
            MoveToANewLocation(door.DoorLocation);
        }
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp19
{
    static class Program
    {
        /// <summary>
        /// アプリケーションのメイン エントリ ポイントです。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
    public class Room : Location
    {
        private string decoration;

        public Room(string name, string decoration) : 
            base(name)
        {
            this.decoration = decoration;
        }
        public string Deoraction
        {
            get { return decoration; }
        }
        public override string Description
        {
            get { return base.Description + " " + decoration + "がみえます。"; }
        }
    }
    
    public class Outside : Location
    {
        private bool hot;

        public Outside(string name, bool hot) : base(name)
        {
            this.hot = hot;
        }
        public bool Hot
        {
            get { return hot; }
        }
        public override string Description
        {
            get
            {
                if (hot)
                {
                    return base.Description +" ここはとても暑い。";
                }
                return base.Description;
            }
        }
    }
    
    interface IHasExteriorDoor
    {
        string DoorDescription { get; }
        Location DoorLocation { get; set; }
    }
    
    public class OutsideWithDoor: Outside, IHasExteriorDoor
    {
        private string doorDescription;
        private Location doorLocation;

        public OutsideWithDoor(string name, bool hot, string doorDescription): 
            base (name, hot)
        {
            this.doorDescription = doorDescription;
        }
        public string DoorDescription
        {
            get { return doorDescription; }
        }
        public Location DoorLocation
        {
            get { return doorLocation; }
            set { doorLocation = value; }
        }
    }
    
    public class RoomWithDoor: Room, IHasExteriorDoor
    {

        private string doorDescription;
        private Location doorLocation;

        public RoomWithDoor(string name, string description,
            string doorDescription):
            base(name, description)
        {
            this.doorDescription = doorDescription;
        }

        public string DoorDescription
        {
            get { return doorDescription; }
        }
        public Location DoorLocation
        {
            get { return doorLocation; }
            set { doorLocation = value; }
        }
    }
}

0 コメント:

コメントを投稿