C#的事件

using System;
using System.Windows.Forms;

class RunClass
{
	public static void Main()
	{
		UserInputMonitor inputMonitor = new UserInputMonitor();
		MessageDisplayer inputProcessor = new MessageDisplayer(inputMonitor);
		ManagersStaffMonitor mortimer = new ManagersStaffMonitor(inputMonitor);
		inputMonitor.Run();
	}
}

class UserInputMonitor
{
	public delegate void UserRequest(object objSender, UserRequestEventArgs e);
	public event UserRequest OnUserRequest;

	public void Run()
	{
		bool bFinished = false;

		while (!bFinished)
		{
			Console.WriteLine("Select preferred option:");
			Console.WriteLine("  Request advertisement - hit A then return");
			Console.WriteLine("  Request personal message from Mortimer - hit P then return");
			Console.WriteLine("  Exit - hit X then return");

			string	strInput = Console.ReadLine();
			char	cRead = (strInput == "") ? ' ' : char.ToUpper(strInput[0]);

			switch (cRead)
			{
				case 'X':
					bFinished = true;
					break;

				case 'A':
					OnUserRequest(this, new UserRequestEventArgs(RequestType.AdRequest));
					break;

				case 'P':
					OnUserRequest(this, new UserRequestEventArgs(RequestType.PersonalMessageRequest));
					break;
			}
		}
	}
}

enum RequestType 
{
	AdRequest,
	PersonalMessageRequest
}

class UserRequestEventArgs: EventArgs
{
	private RequestType request;
	public UserRequestEventArgs(RequestType request)
		:base()
	{
		this.request = request;
	}

	public RequestType Request
	{
		get
		{
			return request;
		}
	}
}

class MessageDisplayer
{
	public MessageDisplayer(UserInputMonitor monitor)
	{
		monitor.OnUserRequest += new UserInputMonitor.UserRequest(UserRequestHandler);
	}

	protected void UserRequestHandler(object objSender, UserRequestEventArgs e)
	{
		switch (e.Request)
		{
			case RequestType.AdRequest:
				Console.WriteLine("Moniter....");
				break;

			case RequestType.PersonalMessageRequest:
				Console.WriteLine("Personal Message Request");
				break;
		}
	}
}

class ManagersStaffMonitor
{
	public ManagersStaffMonitor(UserInputMonitor monitor)
	{
		monitor.OnUserRequest += new UserInputMonitor.UserRequest(UserRequestHandler);
	}

	protected void UserRequestHandler(object objSender, UserRequestEventArgs  e)
	{
		if (e.Request == RequestType.PersonalMessageRequest)
		{
			MessageBox.Show("Hello, andylin", "Monitor");
		}
	}
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值