【C# 进阶】事件!直接上事件!

http://www.tracefact.net/csharp-programming/delegates-and-events-in-csharp.aspx

ZiYang 张,何许人也?看了他写的博客,基本看明白了,就有一句不明白,不过以后再慢慢明白~

事件,卡住了多少英雄好汉向C#继续深入?

对委托的封装!弄清

谁发布消息?

谁订阅消息?消息里传递什么信息给观察者?

收到消息要干哈?

不说他提到的水壶例子了。我想了一个模型,不知道能否实现。

我们的微软客户小吴发布测试的任务消息,我们的测试小队长超超订阅这个消息,背后的大boss小马也订阅这个消息。

小队长超超接收消息后,会向他的队友——我们,发布这个消息,让我们开始干活~

  二级事件。。。我觉得是,不过我没写过。我们订阅超超的有任务消息,我们收到该消息,会开始做该任务。

大Boss小马收到该消息,会回复一声,我知道了,测试小队做了这个任务呀~

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace VenderTest
{
class Program
{
static void Main(string[] args)
{
Customer vsCustomer = new Customer(); vsCustomer.Name = "Wu";
vsCustomer.SentTasks += (new TeamLead() { Name = "Chao" }).ForwardCustomerTask;
vsCustomer.SentTasks += (new Boss() { Name = "Ma" }).ReplyTeamTask;
vsCustomer.MakeTestPlan(, ); }
}
public abstract class Person
{
public string Name { get; set; }
public virtual void Introduction()
{
Console.WriteLine("Hello, my name is {0}", Name);
}
}
public class Customer : Person
{
private string TaskName;
public delegate void SendTaskEventHandler(Object sender, SendTaskEventArgs e);
public event SendTaskEventHandler SentTasks;
public class SendTaskEventArgs : EventArgs
{
public readonly string taskMessage;
public readonly DateTime ETA;
public SendTaskEventArgs(string task, DateTime deadline)
{
this.taskMessage = task;
this.ETA = deadline;
}
}
public virtual void OnSentTasks(SendTaskEventArgs e)
{
SentTasks?.Invoke(this, e);
}
/// <summary>
///
/// </summary>
/// <param name="TaskCount"></param>
/// <param name="TaskInterval">Unit: Days</param>
public void MakeTestPlan(int TaskCount, int TaskInterval)
{
Introduction();
Console.WriteLine("I'm making test plans.");
Console.WriteLine();
for (int i = ; i <= TaskCount; i++)
{
TaskName = "Testing Gallery:" + i.ToString();
DateTime dl = DateTime.Now.AddDays(i * TaskInterval);
SendTaskEventArgs args = new SendTaskEventArgs(TaskName, dl);
OnSentTasks(args);
} }
public override void Introduction()
{
Console.WriteLine("Hello, I'm a vendor customer. My name is {0}", Name); }
}
public class TeamLead :Person
{
public override void Introduction()
{
Console.WriteLine("Hello, I'm a vendor test lead. My name is {0}", Name);
}
public void ForwardCustomerTask(Object sender, Customer.SendTaskEventArgs e)
{
Introduction();
Customer customer = (Customer)sender;
Console.WriteLine("Got a task from the customer: {0}", customer.Name);
//Get information from the event message.
Console.WriteLine("The task name is {0}", e.taskMessage);
Console.WriteLine("The task deadline is {0}", e.ETA);
Console.WriteLine(); }
}
public class Boss:Person
{
private static List<string> tasks = new List<string>();
public override void Introduction()
{
Console.WriteLine("Hello, I'm the manager of vendor team. My name is {0}", Name);
}
public void ReplyTeamTask(Object sender,Customer.SendTaskEventArgs e)
{
Introduction();
Customer customer = (Customer)sender;
Console.WriteLine("OK, {0}. Let's review what they got recently:", customer.Name);
tasks.Add(e.taskMessage);
foreach(var task in tasks)
{
Console.WriteLine(task);
}
Console.WriteLine("Vendors did a lot of tasks. Consider cut some tasks next month.");
Console.WriteLine(); }
} }
上一篇:SQL 2008升级SQL 2008 R2完全教程或者10.00.4000升级10.50.1600


下一篇:ArcGIS中的坐标系定义与转换 (转载)