zoukankan      html  css  js  c++  java
  • 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();
            }
        }
  • 相关阅读:
    Redis-其他命令
    Redis-发布与订阅
    C#使用命令编译代码
    Redis有序集合操作
    Redis散列操作
    设置ul水平居中
    Redis集合操作
    Redis列表操作
    java连SQLServer失败 java.lang.ClassNotFoundException:以及 javax.xml.bind.JAXBException
    SQLServer 用法简例
  • 原文地址:https://www.cnblogs.com/tonymu/p/2889441.html
Copyright © 2011-2022 走看看