Skip to content

用代码表示如下情景:猫叫、老鼠逃跑、主人惊醒

🏷️ C# 学习 C#

1. 定义猫类型,并且该类型负责维护猫叫事件 Cat.cs

点击查看代码
csharp
using System;

namespace CatCry
{
    /// <summary>
    /// 猫类型,维护猫叫事件
    /// </summary>
    public class Cat
    {
        /// <summary>
        /// 猫名
        /// </summary>
        private String _name;
        /// <summary>
        /// 猫叫的事件
        /// </summary>
        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);
        }
    }
}

2. 定义猫叫事件的参数 CatCryEventArgs.cs

点击查看代码
csharp
using System;

namespace CatCry
{
    /// <summary>
    /// 猫叫事件的参数
    /// </summary>
    public class CatCryEventArgs : EventArgs
    {
        /// <summary>
        /// 发出叫声的猫的名字
        /// </summary>
        private string _catname;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="catname"></param>
        public CatCryEventArgs(string catname) : base()
        {
            _catname = catname;
        }
        /// <summary>
        /// 输出参数内容
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return _catname + "叫了";
        }
    }
}

3. 定义猫叫事件的订阅者,即主人类型和老鼠类型

Master.cs

点击查看代码
csharp
using System;

namespace CatCry
{
    /// <summary>
    /// 主人类型
    /// </summary>
    public class Master
    {
        /// <summary>
        /// 主人名字
        /// </summary>
        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="e"></param>
        private void CatCryHandler(object sender, CatCryEventArgs e)
        {
            WakeUp();
        }
        /// <summary>
        /// 惊醒方法
        /// </summary>
        private void WakeUp()
        {
            Console.WriteLine(_name + "醒了");
        }
    }
}

Mouse.cs

点击查看代码
csharp
using System;

namespace CatCry
{
    /// <summary>
    /// 老鼠类型
    /// </summary>
    public class Mouse
    {        
            /// <summary>
        /// 老鼠名字
        /// </summary>
        private string _name;
        /// <summary>
        /// 构造方法,订阅事件
        /// </summary>
        /// <param name="name"></param>
        /// <param name="cat"></param>
        public Mouse(string name, Cat cat)
        {
            _name = name;
            cat.CatCryEvent += CatCryHandler;
        }
        /// <summary>
        /// 猫叫事件处理方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CatCryHandler(object sender, CatCryEventArgs e)
        {
            Run();
        }
        /// <summary>
        /// 逃跑方法
        /// </summary>
        private void Run()
        {
            Console.WriteLine(_name + "逃走了");
        }
    }
}

4. 编写入口方法,模拟场景 Program.cs

点击查看代码
csharp
using System;

namespace CatCry
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("开始模拟");
            Cat cat = new Cat("汤姆猫");
            Mouse mouse1 = new Mouse("米奇", cat);
            Mouse mouse2 = new Mouse("杰瑞", cat);
            Master master = new Master("巴金斯", cat);
            cat.CatCry();
            Console.Read();
        }
    }
}