zoukankan      html  css  js  c++  java
  • asp.net事件委托易理解实例

    比如说一个公司(场景),你是老板,手下有两个员工,小张和小王。

    你命令小王,如果小张玩游戏,则小王扣去小张500元钱。
    这就是现实中的委托。
    实际上,在写程序中,程序员就是老板,小张和小王就是两个对象。小张玩游戏是一个方法,小张还有一个游戏事件,他玩游戏激发这个事件。而小王就是事件处理对象,他负责把小张的钱扣除500。
    所以,委托有如下几个要素:
    1 激发事件的对象--就是小张
    2 处理对象事件的对象--就是小王
    3 定义委托,就是你让小王监视小张。
    如果这三个要素都满足的话,则你就写出了一个完整事件的处理。
    下面有个例子:在vs.net2003 C#控制台应用程序编辑运行成功:
    using System;
    namespace CSharpConsole
    {
      public class 场景
      {
        [STAThread]
        public static void Main(string[] args)
        {
          Console.WriteLine("场景开始了....");
          // 生成小王
          小王 w = new 小王();
          // 生成小账
          小张 z = new 小张();

             // 指定监视

          z.PlayGame += new PlayGameHandler(w.扣钱);
          // 开始玩游戏
          z.玩游戏();
          console.writeline("场景结束...");
          Console.ReadLine();
        }
      }

      // 负责扣钱的人
      public class 小王
      {
        public 小王()
        {
          Console.WriteLine("生成小王...");
        }
        public void 扣钱(object sender,EventArgs e)
        {

          Console.WriteLine("小王:好小子,上班时间胆敢玩游戏...");

          Console.WriteLine("小王:看看你小子有多少钱...");
          小张 f = (小张)sender;
          Console.WriteLine("小张的钱: " + f.钱.ToString());
          Console.WriteLine("开始扣钱......");
          System.Threading.Thread.Sleep(500);
          f.钱 = f.钱 - 500;
          Console.WriteLine("扣完了....现在小张还剩下:" + f.钱.ToString());
        }
      }
      // 如果玩游戏,则引发事件
      public class 小张
      {
        // 先定义一个事件,这个事件表示“小张”在玩游戏。
        public event PlayGameHandler PlayGame;
        // 保存小张钱的变量
        private int m_Money;
        public 小张()
        {
          Console.WriteLine("生成小张....");
          m_Money = 10000; // 构造函数,初始化小张的钱。
        }
        public int 钱 // 此属性可以操作小张的钱。
        {
          get
          {
            return m_Money;
          }
          set
          {
            m_Money = value;
          }
        }
        public void 玩游戏()
        {
          Console.WriteLine("小张开始玩游戏了.....");
          Console.WriteLine("小张:CS好玩,哈哈哈! 我玩.....");
          System.Threading.Thread.Sleep(500);
          System.EventArgs e = new EventArgs();
          //OnPlayGame(e);
          if(PlayGame != null)
          {
            PlayGame(this,e);
          }
        }
        protected virtual void OnPlayGame(EventArgs e)
        {
          if(PlayGame != null)
          {
            PlayGame(this,e);
          }
        }
      }
      // 定义委托处理程序
      public delegate void PlayGameHandler(object sender,System.EventArgs e);
    }

  • 相关阅读:
    leetcode5 Longest Palindromic Substring
    leetcode17 Letter Combinations of a Phone Number
    leetcode13 Roman to Integer
    leetcode14 Longest Common Prefix
    leetcode20 Valid Parentheses
    leetcode392 Is Subsequence
    leetcode121 Best Time to Buy and Sell Stock
    leetcode198 House Robber
    leetcode746 Min Cost Climbing Stairs
    tomcat下使用druid配置jnid数据源
  • 原文地址:https://www.cnblogs.com/keyyang/p/3799041.html
Copyright © 2011-2022 走看看