事件模拟场景经典例子
编辑器加载中...
using System;
using System.Collections.Generic;
using System.Text;
namespace NET.MST.Sixth.CatCry
{
class MainClass
{
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();
}
}
#region cat
/// <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);
}
}
/// <summary>
/// 猫叫事件的参数
/// </summary>
public class CatCryEventArgs : EventArgs
{
//发出叫声的猫的名字
private String _catname;
public CatCryEventArgs(String catname):base()
{
_catname = catname;
}
/// <summary>
/// 输出参数内容
/// </summary>
/// <returns></returns>
public override string ToString()
{
return _catname + "叫了";
}
}
#endregion
#region Master
/// <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="args"></param>
private void CatCryHandler(object sender, CatCryEventArgs args)
{
WakeUp();
}
/// <summary>
/// 惊醒方法
/// </summary>
private void WakeUp()
{
Console.WriteLine(_name + "醒了");
}
}
#endregion
#region mouse
/// <summary>
/// 老鼠类型
/// </summary>
public class Mouse
{
/// <summary>
/// 老鼠名字
/// </summary>
private String _name;
public Mouse(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)
{
Run();
}
/// <summary>
/// 逃跑方法
/// </summary>
private void Run()
{
Console.WriteLine(_name + "逃走了");
}
}
#endregion
}
using System.Collections.Generic;
using System.Text;
namespace NET.MST.Sixth.CatCry
{
class MainClass
{
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();
}
}
#region cat
/// <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);
}
}
/// <summary>
/// 猫叫事件的参数
/// </summary>
public class CatCryEventArgs : EventArgs
{
//发出叫声的猫的名字
private String _catname;
public CatCryEventArgs(String catname):base()
{
_catname = catname;
}
/// <summary>
/// 输出参数内容
/// </summary>
/// <returns></returns>
public override string ToString()
{
return _catname + "叫了";
}
}
#endregion
#region Master
/// <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="args"></param>
private void CatCryHandler(object sender, CatCryEventArgs args)
{
WakeUp();
}
/// <summary>
/// 惊醒方法
/// </summary>
private void WakeUp()
{
Console.WriteLine(_name + "醒了");
}
}
#endregion
#region mouse
/// <summary>
/// 老鼠类型
/// </summary>
public class Mouse
{
/// <summary>
/// 老鼠名字
/// </summary>
private String _name;
public Mouse(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)
{
Run();
}
/// <summary>
/// 逃跑方法
/// </summary>
private void Run()
{
Console.WriteLine(_name + "逃走了");
}
}
#endregion
}
广积粮,筑高墙,缓称王