• C#事件机制复习


    事件机制
        class Person
        {
    
            public string name;
            public int age;
            public string sex;
            public float money;
            public Person()
            {
                this.name = "张三";
                this.age = 18;
                this.sex = "";
                this.money = 1000;
            }
            public Person(string name, int age, string sex,float money)
            {
                this.name = name;
                this.age = age;
                this.sex = sex;
                this.money = money;
            }
    
        }
        class Custom : Person
        {
           public delegate void BuyEventHandler(object sender);
           public event BuyEventHandler BuyEvent; 
    
           public void Buy()
           {
               if(this.money==0)
                   BuyEvent(this);
           }
        }
        class Seller:Person
        {
           
            public Seller(Custom c)
            {
                c.BuyEvent += new Custom.BuyEventHandler(this.Warning);
            }
    
            public void Warning(object sender)
            {
               
                Console.WriteLine("没钱了请充值!");
            }
        }
        
    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    // 定义事件包含数据 
    public class MyEventArgs : EventArgs
    {
        private string StrText;
        public MyEventArgs(string StrText)
        {
            this.StrText = StrText;
        }
        public string GetStrText
        {
            get
            {
                return StrText;
            }
        }
    }
    // 发布事件的类 
    class EventSource
    {
        MyEventArgs EvArgs = new MyEventArgs("触发事件");
        // 定义委托 
        public delegate void EventHandler(object sender, MyEventArgs e);
        // 定义事件 
        public event EventHandler TextOut;
        // 激活事件的方法 
        public void TriggerEvent()
        {
            if (TextOut == null)
                TextOut(this, EvArgs);
        }
    } 
    // 订阅事件的类 
    class TestApp
    {
        public static void Main()
        {
            EventSource evsrc = new EventSource();
            // 订阅事件 
            evsrc.TextOut += new EventSource.EventHandler(CatchEvent);
            // 触发事件 
            evsrc.TriggerEvent();
            Console.WriteLine("------");
            // 取消订阅事件 
            evsrc.TextOut -= new EventSource.EventHandler(CatchEvent);
            // 触发事件 
            evsrc.TriggerEvent();
            Console.WriteLine("------");  // 事件订阅已取消,什么也不执行 
            TestApp theApp = new TestApp();
            evsrc.TextOut += new EventSource.EventHandler(theApp.InstanceCatch);
            evsrc.TriggerEvent();
            Console.WriteLine("------");
        }
        // 处理事件的静态方法 
        public static void CatchEvent(object from, MyEventArgs e)
        {
            Console.WriteLine("CatchEvent:{0}", e.GetStrText);
        }
        // 处理事件的方法 
        public void InstanceCatch(object from, MyEventArgs e)
        {
            Console.WriteLine("InstanceCatch:{0},e.GetStrText");
        }
    }
  • 相关阅读:
    resolv.conf 是什么
    Cloudera Manager Free Edition 4.1 和CDH 4.1.2 简易安装教学
    bach cello
    windows 7 旗舰版 切换 中英文 界面
    geghi 教程 以lesmiserable 为例
    python 使用 tweepy 案例: PS4
    用Live Writer写博文插入程序代码的三个插件比较
    侧滑菜单
    [Android]简略的Android消息机制源码分析
    常用git命令
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/2998307.html
Copyright © 2020-2023  润新知