• C#委托 委托示例 事件驱动


     
       
    C#委托 委托示例 事件驱动
     

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    using System.Runtime.Remoting.Messaging;
    using System.IO;
    using System.Net;

    namespace ConsoleApplication2
    {
         /// <summary>
         /// *************************************************************************
         /// 程序作者:李伟涛
         /// QQ:12666954
         /// 传递消息,驱动事件
         /// *************************************************************************
         /// </summary>
        
         public struct Quitperson//定义逃课人的信息
         {
             public string name;
             public string Class;
         }
         class client
         {
             public class CQuithireEventArgs:EventArgs
             {
                 public readonly Quitperson quitperson;
                 public CQuithireEventArgs(Quitperson quitperson)//封装事件信息
                 {
                     this.quitperson=quitperson;
                 }
             }
             public delegate void QuithireEventHandler(object sender, CQuithireEventArgs e);//定义委托
             public event QuithireEventHandler OnQuithire;//事件
             protected virtual void Quithire(CQuithireEventArgs e)//事件通知器
             {
                 if (OnQuithire != null)
                     OnQuithire(this, e);
             }
             public void setQuitperson(Quitperson quitperson)
             {
                 CQuithireEventArgs e = new CQuithireEventArgs(quitperson);
                 Quithire(e);//调用通知器,通知所有登记事件的订阅着
             }
         }
         class teacher
         {
             public void report(object sender, client.CQuithireEventArgs e)
             {
                 Console.WriteLine("{0}班的{1}逃课了!", e.quitperson.Class, e.quitperson.name);
             }
             public teacher(client clt)
             {
                 clt.OnQuithire += new client.QuithireEventHandler(report);
             }
         }
         class schoolposident
         {
             public void Sreport(object sender, client.CQuithireEventArgs e)
             {
                 Console.WriteLine("{0}:{1}班的{2}逃课了!", DateTime.Now.ToShortDateString(), e.quitperson.Class, e.quitperson.name);

             }
             public schoolposident(client clt)
             {
                 clt.OnQuithire += new client.QuithireEventHandler(Sreport);
             }
         }
         class Program
         {
             static void Main(string[] args)
             {
                 Quitperson q = new Quitperson();//初始化逃课人信息
                 q.Class = "软件051";
                 q.name = "李伟涛";
                 client clt = new client();
                 //订阅事件
                 teacher t = new teacher(clt);
                 schoolposident sp = new schoolposident(clt);
                 clt.setQuitperson(q);
                 Console.ReadLine();

             }
         }
       
    }

  • 相关阅读:
    JavaScript中常见的十五种设计模式
    如何使 highchart图表标题文字可选择复制
    python如何以表格形式打印输出
    记一次vue长列表的内存性能分析和优化
    如何收集常见的前端性能信息
    如何实现同等间隙的卡片布局
    图片横向等高瀑布流,每行占满,限制行数 的实现
    弹窗查看内容时 内容滚动区域设置为body区
    卡片列表项缓缓往下展示 效果实现
    【年终总结】微信前端社招有感
  • 原文地址:https://www.cnblogs.com/zwq194/p/1492186.html
Copyright © 2020-2023  润新知