delegate和event的區別
阿新 • • 發佈:2019-01-28
using System;
namespace nsEventSample
{
///<summary>/// 類EatEventArgs必須繼承自類EventArgs,用來引發事件時封裝資料
///</summary>publicclass EatEventArgs : EventArgs
{
public String restrauntName; //飯店名稱publicdecimal moneyOut; //準備消費金額 }
///<summary>/// 這個委託用來說明處理吃飯事件的方法的方法頭(模式)
///</summary>publicdelegatevoid EatEventHandler(object sender, EatEventArgs e);
///<summary>/// 引發吃飯事件(EateEvent)的類Master(主人),這個類必須
/// 1.宣告一個名為EatEvent的事件:public event EatEventHandler EatEvent;
/// 2.通過一個名為OnEatEvent的方法來引發吃飯事件,給那些處理此事件的方法傳資料;
/// 3.說明在某種情形下引發事件呢?在餓的時候。用方法Hungrg來模擬。
///</summary>publicclass Master
{
//宣告事件publicevent EatEventHandler EatEvent;
//引發事件的方法publicvoid OnEatEvent(EatEventArgs e)
{
if (EatEvent !=null)
{
EatEvent(this, e);
}
}
//當主人餓的時候,他會指定吃飯地點和消費金額。publicvoid Hungry(String restrauntName, decimal moneyOut)
{
EatEventArgs e =new EatEventArgs();
e.restrauntName = restrauntName;
e.moneyOut = moneyOut;
Console.WriteLine("主人說:");
Console.WriteLine("我餓了,要去{0}吃飯,消費{1}元", e.restrauntName, e.moneyOut);
//引發事件 OnEatEvent(e);
}
}
///<summary>/// 類Servant(僕人)有一個方法ArrangeFood(安排食物)來處理主人的吃飯事件
///</summary>publicclass Servant
{
publicvoid ArrangeFood(object sender, EatEventArgs e)
{
Console.WriteLine();
Console.WriteLine("僕人說:");
Console.WriteLine("我的主人, 您的命令是 : ");
Console.WriteLine("吃飯地點 -- {0}", e.restrauntName);
Console.WriteLine("準備消費 -- {0}元 ", e.moneyOut);
Console.WriteLine("好的,正給您安排。。。。。。。。");
Console.WriteLine("主人,您的食物在這兒,請慢用");
}
}
///<summary>/// 類God安排qinshihuang(秦始皇)的僕人是lisi(李斯),並讓李斯的ArrangeFood
/// 方法來處理qinshihuang的吃飯事件:qinshihuang.EatEvent += new EatEventHandler(lishi.ArrangeFood);
///</summary>publicclass God
{
publicstaticvoid Main()
{
Master qinshihuang =new Master();
Servant lishi =new Servant();
qinshihuang.EatEvent +=new EatEventHandler(lishi.ArrangeFood);
//秦始皇餓了,想去希爾頓大酒店,消費5000元 qinshihuang.Hungry("希爾頓大酒店", 5000.0m);
Console.ReadLine();
}
}
}
namespace nsEventSample
{
///<summary>/// 類EatEventArgs必須繼承自類EventArgs,用來引發事件時封裝資料
///</summary>publicclass EatEventArgs : EventArgs
{
public String restrauntName; //飯店名稱publicdecimal moneyOut; //準備消費金額 }
///<summary>/// 這個委託用來說明處理吃飯事件的方法的方法頭(模式)
///<summary>/// 引發吃飯事件(EateEvent)的類Master(主人),這個類必須
/// 1.宣告一個名為EatEvent的事件:public event EatEventHandler EatEvent;
/// 2.通過一個名為OnEatEvent的方法來引發吃飯事件,給那些處理此事件的方法傳資料;
/// 3.說明在某種情形下引發事件呢?在餓的時候。用方法Hungrg來模擬。
{
//宣告事件publicevent EatEventHandler EatEvent;
//引發事件的方法publicvoid OnEatEvent(EatEventArgs e)
{
if (EatEvent !=null)
{
EatEvent(this, e);
}
}
//當主人餓的時候,他會指定吃飯地點和消費金額。publicvoid Hungry(String restrauntName,
{
EatEventArgs e =new EatEventArgs();
e.restrauntName = restrauntName;
e.moneyOut = moneyOut;
Console.WriteLine("主人說:");
Console.WriteLine("我餓了,要去{0}吃飯,消費{1}元", e.restrauntName, e.moneyOut);
//引發事件 OnEatEvent(e);
}
}
///<summary>/// 類Servant(僕人)有一個方法ArrangeFood(安排食物)來處理主人的吃飯事件
///</summary>publicclass Servant
{
publicvoid ArrangeFood(object sender, EatEventArgs e)
{
Console.WriteLine();
Console.WriteLine("僕人說:");
Console.WriteLine("我的主人, 您的命令是 : ");
Console.WriteLine("吃飯地點 -- {0}", e.restrauntName);
Console.WriteLine("準備消費 -- {0}元 ", e.moneyOut);
Console.WriteLine("好的,正給您安排。。。。。。。。");
Console.WriteLine("主人,您的食物在這兒,請慢用");
}
}
///<summary>/// 類God安排qinshihuang(秦始皇)的僕人是lisi(李斯),並讓李斯的ArrangeFood
/// 方法來處理qinshihuang的吃飯事件:qinshihuang.EatEvent += new EatEventHandler(lishi.ArrangeFood);
///</summary>publicclass God
{
publicstaticvoid Main()
{
Master qinshihuang =new Master();
Servant lishi =new Servant();
qinshihuang.EatEvent +=new EatEventHandler(lishi.ArrangeFood);
//秦始皇餓了,想去希爾頓大酒店,消費5000元 qinshihuang.Hungry("希爾頓大酒店", 5000.0m);
Console.ReadLine();
}
}
}