c# 创建委托 消息订阅_C#中的委托、事件及事件的订阅

本文介绍了如何在C#中创建和使用委托、事件以及订阅事件。通过定义价格事件参数类PriceChangedEventArgs、价格变化委托PriceChangedEventHandle以及Shop类中的PriceChanged事件,展示了事件在类间通信的角色。示例中详细展示了Shop对象如何引发PriceChanged事件,Customer对象如何订阅和处理这些事件,以及事件订阅的添加、移除过程。
摘要由CSDN通过智能技术生成

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

//P80

//事件建立在委托之上,通过该机制,某个类在发生某些特定的事情之后,通知其他类或对象正在发生的事情。

//1.定义价格事件的参数类--PriceChangedEventArgs;

//2.然后,定义该时间段额处理委托类型----PriceChangedEventHander;

//3.在Shop类中定义具体的事件----PiceChanged,它实际上就是PriceChangedEventHander类型的委托;

//4.在OnPriceChanged()中引发PriceChanged事件,将当前类作为事件的发生者,在引发事件之前先判断是否已经注册。

//---------------------------------------------------------------------------------------------------------

namespace Uing_event

{

class Program

{

//①定义价格事件的参数类--PriceChangedEventArgs

public class PriceChangedEventArgs : EventArgs

{

//构造函数

public PriceChangedEventArgs(string nm, float pr)

{

this._Name = nm;

this._Price = pr;

}

//价格变化的商品名称

private string _Name;

public string Name

{

get

{

return this._Name;

}

set

{

this._Name = value;

}

}

//价格变化之后的商品价格

private float _Price;

public float Price

{

get

{

return this._Price;

}

}

}

//②定义价格变化的委托类型

public delegate void PriceChangedEventHandle(object sender,PriceChangedEventArgs e);//定义事件处理的委托类型 △

//商店类,价格变化事件的发生类

//-------------------------------------------------------------------------------------------------------

public class Shop

{

//③通过event关键字定义价格变化事件

public event PriceChangedEventHandle PriceChanged;//它实际上就是PriceChangedEventHander类型的委托

//引发事件函数

protected void OnPriceChanged(PriceChangedEventArgs arg)//在该函数中直接引发PriceChanged事件

{

//如果事件已经注册,则通过委托调用函数的方式通知事件订阅用户

if (this.PriceChanged!=null)//weng:如果事件被订阅

{

this.PriceChanged(this,arg);

}

}

//更新商品名称,并引发商品价格变化事件

public void UpdatePrice(string nm, float newPrice)

{

//创建PriceChanged事件参数

PriceChangedEventArgs arg = new PriceChangedEventArgs(nm, newPrice);

//引发PriceChanged事件

this.OnPriceChanged(arg);

}

public Shop(string nm)

{

this._Name = nm;

}

//商品名称

private string _Name;

public string Name

{

get { return this._Name; }

}

}//end class Shop

//客户类,用于订阅PriceChanged事件

//-------------------------------------------------------------------------------------------------------

public class Customer

{

public Customer(string nm)

{

this._Name = nm;

}

//顾客姓名

private string _Name;

public string Name

{

get { return this._Name; }

}

//PriceChanged事件的响应函数

public void Shop_PriceChanged(object sender, PriceChangedEventArgs e)

{

//将sender转换为Shop类型变量,并打印提示信息

Shop sp = sender as Shop;

if (sp != null)

System.Console.WriteLine("{0}收到{1}:{2}新的价格为{3}元",this._Name,sp.Name,e.Name,e.Price);

}

}

static void Main(string[] args)

{

//定义两个Shop对象,用于引发事件

Shop shop1 = new Shop("南京和平电影城");

Shop shop2 = new Shop("钱塘园");

//定义两个Customer对象,用来订阅并处理事件

Customer cust1 = new Customer("WENG LIU");

Customer cust2 = new Customer("Customer1");

//cust1订阅Shop1的PriceChanged事件,并使用自己的处理函数Shop_PriceChanged处理该事件。

//顾客订阅哪家店铺的事件,实际上就是将本人对该商店的那个事件的处理函数加入那家商店的委托类型变量函数链中

System.Console.WriteLine("1.shop1.UpdataPrice(\"Goods1\",2.2f)");

shop1.PriceChanged += cust1.Shop_PriceChanged;

//shop1更新商品价格,引发PriceChanged事件

shop1.UpdatePrice("Goods1",2.5f);

//cust2订阅Shop2的PriceChanged事件,并使用自己的处理函数Shop_PriceChanged处理该事件。

System.Console.WriteLine("2.shop2.UpdataPrice(\"Goods2\",5.6f)");

shop2.PriceChanged += cust2.Shop_PriceChanged;

//shop1更新商品价格,引发PriceChanged事件

shop2.UpdatePrice("Goods2", 5.6f);

//cust2订阅shop1的shop1的PriceChanged事件

shop1.PriceChanged += cust2.Shop_PriceChanged;

//shop1更新商品价格,引发PriceChanged事件

System.Console.WriteLine("3.shop1.UpdataPrice(\"Goods3\",1.6f)");

shop1.UpdatePrice("Goods3",1.6f);

//csut1取消订阅shop1的PriceChanged事件

shop1.PriceChanged -= cust1.Shop_PriceChanged;

//shop1更新商品价格,引发PriceChanged事件

System.Console.WriteLine("4.shop1.UpdataPrice(\"Goods4\",0.5f)");

shop1.UpdatePrice("Goods4", 1.6f);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值