事件与委托

//1.顾客去书店订购某种类型(计算机)的书,当书店新到某类型的书籍,会通知需要此类书的顾客。

//2.当顾客的需求类型发生变化时,需要通知书店。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{

    public class Customer
    {
        public string name;
        public string type;

        public Customer(string m_name, string m_type)
        {
            name = m_name;
            type = m_type;
        }

        public void Register(BookStore store)
        {
          
            store.OnNewBook += new EventHandler(store_OnNewBook);
            BookStore.list.Add(this);

        }

        void store_OnNewBook(object sender, EventArgs e)
        {
            MyEventhandler temphandler = (MyEventhandler)e;

            if (temphandler.m_type == type)
            {
                Console.WriteLine("{0}:你好!,本店新近《{1}》", name, temphandler.m_name);
            }
        }

        public event EventHandler OnHandPro;

        public void HandPro()
        {
            MyEventhandler myhandler = new MyEventhandler(this.name, this.type);
            OnHandPro(this, myhandler);       
        }
    }

    public class MyEventhandler : EventArgs
    {
        public string m_name;
        public string m_type;

        public MyEventhandler(string name, string type)
        {
            m_name = name;
            m_type = type;
        }
    }
    public class BookStore
    {
        public static List<Customer> list = new List<Customer>();

        public event EventHandler OnNewBook;

        public void NewBook(string m_name, string m_type)
        {
            MyEventhandler myevethandler = new MyEventhandler(m_name, m_type);
            OnNewBook(this, myevethandler);
        }

        public void Register(Customer cs)
        {
            cs.OnHandPro += new EventHandler(cs_HandPro);
        }

        void cs_HandPro(object sender, EventArgs e)
        {
            Customer cs = (Customer)sender;

            foreach (Customer item in BookStore.list)
            {
                if (item.name == cs.name && item.type != cs.type)
                {
                    Console.WriteLine("书店你好!{0}需求的书籍类型更为{1}",cs.name, cs.type);
                    break;
                }
            }
          
        }
    }

 

    class Program
    {
        static void Main()
        {
            BookStore store = new BookStore();           
            Customer cs1 = new Customer("TOm", "计算机");
            Customer cs2 = new Customer("Jim", "英语");
            cs1.Register(store);
            cs2.Register(store);
            store.NewBook("数据结构", "计算机");
            store.NewBook("英语2", "英语");
            cs1 = new Customer("TOm", "计算机2");
            store.Register(cs1);
            store.Register(cs2);
            cs1.HandPro();
            cs2.HandPro();
            Console.ReadLine();
        }

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#事件委托是密切相关的概念。委托是一种类型,它封装了一个或多个方法,并允许将这些方法作为参数传递给其他方法。事件则是一种特殊的委托,它允许程序员在对象发生特定的操作或状态改变时通知其他对象。 在C#,可以使用delegate关键字定义委托类型。例如: ``` public delegate void MyDelegate(int x, int y); ``` 这个代码定义了一个名为MyDelegate的委托类型,它封装了一个具有两个int参数的方法。可以使用这个委托类型来声明变量,例如: ``` MyDelegate myDelegate = new MyDelegate(MyMethod); ``` 这个代码创建了一个名为myDelegate的变量,它引用了一个具有两个int参数的方法MyMethod。 事件通常使用EventHandler委托类型作为事件处理程序的签名。例如: ``` public class MyClass { public event EventHandler MyEvent; } ``` 这个代码定义了一个名为MyEvent的事件,它使用EventHandler委托类型作为事件处理程序的签名。可以使用“+=”运算符将事件处理程序添加到事件的处理程序列表。例如: ``` MyClass myObject = new MyClass(); myObject.MyEvent += new EventHandler(MyEventHandler); ``` 这个代码将MyEventHandler方法添加到MyClass对象的MyEvent事件的处理程序列表。 需要注意的是,事件只能在事件发布者类声明和触发,而事件处理程序则可以在事件订阅者类定义。事件的订阅者可以使用委托实例来订阅事件,并在事件发生时执行相应的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值