C#面试题

记录一道面试题:

猫叫了,老鼠逃跑了,主人醒了。

public class Program
    {
        public delegate void CryHandler(object sender, CryArgs e);

        public class CryArgs
        {
            /// <summary>
            /// 声音内容
            /// </summary>
            public string Voice { get; set; }

            /// <summary>
            /// 声音强度
            /// </summary>
            public string VoiceLevel { get; set; }
        }

        /// <summary>
        /// 事物基类
        /// </summary>
        public abstract class AnyOne
        {
            /// <summary>
            ////// </summary>
            public abstract void Hear(object sender, CryArgs e);

            /// <summary>
            /// 名字
            /// </summary>
            public string Name { get; set; }
        }

        /// <summary>
        /// 主人
        /// </summary>
        public class Master : AnyOne
        {
            public Master()
            {
                base.Name = "主人";
            }

            /// <summary>
            ////// </summary>
            public override void Hear(object sender, CryArgs e)
            {
                Awake(e);
            }

            /// <summary>
            ////// </summary>
            private void Awake(CryArgs e)
            {
                Console.WriteLine(string.Format("{0}听到猫{2}地叫\"{1}\",被惊醒了!", base.Name, e.Voice, e.VoiceLevel));
            }
        }

        /// <summary>
        /// 老鼠
        /// </summary>
        public class Mouse : AnyOne
        {
            public Mouse()
            {
                base.Name = "老鼠";
            }

            /// <summary>
            ////// </summary>
            public override void Hear(object sender, CryArgs e)
            {
                RunAway(e);
            }

            /// <summary>
            ////// </summary>
            private void RunAway(CryArgs e)
            {
                Console.WriteLine(string.Format("{0}听到猫{2}地叫\"{1}\",被吓跑了!", base.Name, e.Voice, e.VoiceLevel));
            }
        }

        /// <summary>
        ////// </summary>
        public class Cat
        {
            private CryArgs cryArgs;
            public event CryHandler OnCry;

            public Cat()
            {
                cryArgs = new CryArgs();
            }

            /// <summary>
            ////// </summary>
            public virtual void Cry(string voice, string voiceLevel)
            {
                cryArgs.Voice = voice;
                cryArgs.VoiceLevel = voiceLevel;
                Console.WriteLine(string.Format("猫{0}地叫了一声:{1}!", voiceLevel, voice));
                if (OnCry != null)
                {
                    OnCry(this, cryArgs);
                }
            }
        }

        public static void Main(string[] args)
        {
            Cat cat = new Cat();
            Master master = new Master();
            Mouse mouse = new Mouse();

            cat.OnCry += master.Hear;
            cat.OnCry += mouse.Hear;

            cat.Cry("眯眯眯", "大声");

            Console.ReadLine();
        }
    }

 

posted @ 2013-02-01 17:53  小八路  阅读(457)  评论(0编辑  收藏  举报