事件的用法

  • 2242
  • 0
  • C#
  • 2009-12-29

因為有朋友對於事件的用法不太熟悉

所以寫了一下

大致上是模擬開心水族箱

因為有朋友對於事件的用法不太熟悉

所以寫了一下

大致上是模擬開心水族箱

Instance出來的魚有觀察Player的OnThrowFeed

每當Player.OnThrowFeed的時候就會計算自己離飼料多遠

flsh.cs


using System;

namespace EventHandler
{
    class Fish
    {
        int x;
        int y;
        string name;
        public string Name
        {
            get
            {
                return this.name;
            }
        }
        public int X
        {
            get
            {
                return this.x;
            }
        }
        public int Y
        {
            get
            {
                return this.y;
            }
        }
        public Fish(string name, Player player, int x, int y)
        {
            this.name = name;
            this.x = x;
            this.y = y;
            player.OnThrowFeed += new ThrowFeeded(player_ThrowFeedHandler);
        }

        void player_ThrowFeedHandler(ThrowFeedArgs args)
        {
            Console.WriteLine(name + "距離飼料的X = " + Math.Abs(args.X - this.x) + ", Y = " + Math.Abs(args.Y - this.y));
        }
    }
}

player.cs


using System;

namespace EventHandler
{
    public delegate void ThrowFeeded(ThrowFeedArgs args);
    class Player
    {
        public event ThrowFeeded OnThrowFeed;
        public Player()
        {

        }

        public void ThrowFeed(ThrowFeedArgs e)
        {
            if (OnThrowFeed != null)
            {
                OnThrowFeed(e);
            }
        }
    }

    public class ThrowFeedArgs : EventArgs
    {
        private int x;
        private int y;
        public int X
        {
            get
            {
                return this.x;
            }
        }
        public int Y
        {
            get
            {
                return this.y;
            }
        }
        public ThrowFeedArgs(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
}

program.cs


using System;

namespace EventHandler
{
    class Program
    {
        static void Main(string[] args)
        {
            ConsoleKeyInfo keyInfo;
            Player player;
            Fish fish1, fish2, fish3, fish4;
            do
            {
                player = new Player();
                Console.WriteLine("input x : ");
                int x = int.Parse(Console.ReadLine());
                Console.WriteLine("input y : ");
                int y = int.Parse(Console.ReadLine());
                Random random = new Random();
                fish1 = new Fish("小笨魚1", player, random.Next(0, 400), random.Next(0, 400));
                fish2 = new Fish("小笨魚2", player, random.Next(0, 400), random.Next(0, 400));
                fish3 = new Fish("小笨魚3", player, random.Next(0, 400), random.Next(0, 400));
                fish4 = new Fish("小笨魚4", player, random.Next(0, 400), random.Next(0, 400));
                player.ThrowFeed(new ThrowFeedArgs(x, y));
                Console.WriteLine("Enter 繼續、Exc 結束");
                keyInfo = Console.ReadKey();
            } while (keyInfo.Key != ConsoleKey.Escape);
        }
    }
}

很簡單的sample

沒有多做sample事件之外的處理