纸上得来终觉浅,绝知此事要躬行。

 

c#经典面试题—猫叫,主人醒,老鼠跑(事件的处理)

实现方法:分3个类实现,分别为猫类,老鼠类,主人类。

猫类:定义一个猫叫事件;

老鼠类:订阅猫叫事件,在猫发出叫声这个事件后,老鼠逃跑;

主人类:类似于老鼠类,在猫发出叫声这个事件后,主人醒来;

猫类实现如下:

using System;

namespace CarCry
{
    ///<summary>
    /// 猫类的定义
    ///</summary>
    public class Cat
    {
        //猫名
        private string _name;
        //猫叫事件
        public event EventHandler<CatCryEventArgs> CatCryEvent;
        ///<summary>
        /// 构造函数
        ///</summary>
        ///<param name="name">名字参数</param>
        public Cat( string name )
        {
            _name = name;
        }

        ///<summary>
        /// 促发猫叫的事件
        ///</summary>
        public void CatCry()
        {
            CatCryEventArgs args = new CatCryEventArgs(_name);
            Console.WriteLine(args);
            CatCryEvent(this, args);
        }
    }

    ///<summary>
    /// 猫叫事件参数
    ///</summary>
    public class CatCryEventArgs:EventArgs
    {
        //发出叫声的猫的名字
        private string _catname;
        ///<summary>
        /// 构造函数
        ///</summary>
        public CatCryEventArgs( string catname ):base()
        {
            _catname = catname;
        }

        ///<summary>
        /// 输出参数内容
        ///</summary>
        public override string ToString()
        {
            return "" + _catname + " 叫了";
        }
    }
}
//老鼠类实现如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace CarCry
{
    public class Mouse
    {
        //老鼠名字
        private string _name;
        ///<summary>
        /// 构造函数
        ///</summary>
        ///<param name="name">老鼠的名字</param>
        ///<param name="cat">发出叫声的猫</param>
        public Mouse( string name, Cat cat )
        {
            _name = name;
            cat.CatCryEvent += CatCryHandle;//订阅猫叫事件
        }

        ///<summary>
        /// 猫叫事件处理
        ///</summary>
        ///<param name="sender"></param>
        ///<param name="args"></param>
        private void CatCryHandle( object sender, CatCryEventArgs args )
        {
            Run();
        }

        ///<summary>
        /// 逃跑方法
        ///</summary>
        private void Run()
        {
            Console.WriteLine("老鼠 " + _name + " 逃跑了");
        }
    }
}
//主人类实现如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace CarCry
{
    public class Master
    {
        //主人名字
        private string _name;

        ///<summary>
        /// 构造函数,订阅事件
        ///</summary>
        ///<param name="name">主人名字</param>
        ///<param name="cat"></param>
        public Master( string name, Cat cat )
        {
            _name = name;
            cat.CatCryEvent += CatCryHandler;//订阅猫叫事件
        }

        ///<summary>
        /// 猫叫事件处理
        ///</summary>
        ///<param name="sender"></param>
        ///<param name="args">猫叫事件</param>
        private void CatCryHandler( object sender, CatCryEventArgs args )
        {
            WakeUp();
        }

        ///<summary>
        /// 主人醒了事件
        ///</summary>
        private void WakeUp()
        {
            Console.WriteLine("主人 " + _name + " 醒了");
        }

    }
}
//主函数的调用如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace CarCry
{
    class MainClass
    {
        static void Main( string[] args )
        {
            Console.WriteLine("开始模拟");
            Cat cat = new Cat("Tom");
            Mouse mouse1 = new Mouse("Jack", cat);
            Mouse mouse2 = new Mouse("jackson", cat);
            Master master = new Master("Tao", cat);
            Master master2 = new Master("Hong", cat);
            cat.CatCry();
            Console.ReadLine();
        }
    }
}

posted on 2011-01-07 18:10  JRoger  阅读(4728)  评论(3编辑  收藏  举报

导航