• C# 委托与事件


    using System;
    
    namespace TheDelegate
    {
        public class People
        {
            public static void Say()
            {
                Console.WriteLine("好的,我说一下,是我代干的哈。");
            }
            public static int Sum(int a, int b)
            {
                int c = a + b;
                Console.WriteLine("结果是:{0}", c);
                return c;
            }
            public static int By(int a, int b)
            {
                int c = a * b;
                Console.WriteLine("结果是:{0}", c);
                return c;
            }
    
            public delegate void SmileEventHandler();
            public event SmileEventHandler OnSmile;
            public void Smile()
            {
                if (OnSmile != null)
                {
                    Console.WriteLine("I'm smiling...");
                    OnSmile();
                }
            }
            public delegate void LaougthEventHandler(object sender, EventArgs e);
            public event LaougthEventHandler OnLaougth;
            public void Laougth()
            {
                if (OnLaougth != null)
                {
                    Console.WriteLine("I'm Laougthing, hahahaha...");
                    OnLaougth(this, new EventArgs());
                }
            }
        }
        class Program
        {
            public delegate void SayEventHandler();
            public delegate int CalEventHandler(int a, int b);
            static void Main(string[] args)
            {
                SayEventHandler say = new SayEventHandler(People.Say);
                say();
                CalEventHandler cal = new CalEventHandler(People.Sum);
                cal += People.By;
                int res = cal(22, 33);
                Console.WriteLine(res);
    
                People ppl = new People();
                ppl.OnSmile += new People.SmileEventHandler(ppl_OnSmile);
                ppl.Smile();
                ppl.Smile();
                ppl.OnLaougth += new People.LaougthEventHandler(ppl_OnLaougth);
                ppl.Laougth();
            }
            private static void ppl_OnSmile()
            {
                Console.WriteLine("严肃!");
            }
            private static void ppl_OnLaougth(object sender, EventArgs e)
            {
                Console.WriteLine("安静,安静!	{0}	{1}", sender, e);
            }
        }
    }
  • 相关阅读:
    利用Telnet来模拟Http请求 有GET和POST两种
    WebConfig特殊字符的转义!
    userprofile同步用户失败的原因和解决方案
    linux mysql表名大小写
    web.py 中文模版报错
    docker 开启远程
    web.py 笔记
    python 安装influxdb-python
    安装pip
    influxdb 命令
  • 原文地址:https://www.cnblogs.com/lged/p/6223827.html
Copyright © 2020-2023  润新知