close

自學筆記,幫助自己記憶,以利未來回想…

情境:貓大叫一聲,老鼠逃跑,主人被驚醒。

 

//自己Copy用

 class Cat

    {

        public event Action MaioHandler; //定義一個委派事件:貓叫聲

        //貓叫

        public void Cry()

        {

            Console.WriteLine("貓叫了");

            if (MaioHandler != null) {

                foreach (Action action in MaioHandler.GetInvocationList()) {

                    action.Invoke();

                }

            }

        }

    }

 

    class Human

    {

        //主人醒

        public void Wake()

        {

            Console.WriteLine("主人醒了!");

        }

    }

 

    class Mouse

    {

        //老鼠跑

        public void Run()

        {

            Console.WriteLine("老鼠跑了!");

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            Cat cat = new Cat();

            cat.MaioHandler += new Mouse().Run;

            cat.MaioHandler += new Human().Wake;

            cat.Cry();

 

            Console.Read();

        }

arrow
arrow

    以戒為師 發表在 痞客邦 留言(0) 人氣()