当前位置:首页 > 代码相关 > 正文内容

C#事件_Sample_1

admin6年前 (2020-03-28)代码相关8235

事件模型的五个组成部分:

1、事件的拥有者(event source,只能是对象或类)

2、事件成员(event,成员)

3、事件的响应者(event subscriber,对象)

4、事件处理器(event handler,成员)--本质上是一个回调方法

5、事件订阅--把事件处理器与事件关联在一起

using System;
using System.Timers;

namespace _20200328_事件_Sample_1
{
    class Program
    {
        static void Main(string[] args)
        {
            Timer timer = new Timer();
            timer.Interval = 1000;

            Boy boy = new Boy();
            Girl girl = new Girl();

            timer.Elapsed += boy.Action;
            timer.Elapsed += girl.Action;
            timer.Start();

            Console.ReadKey();
        }

    }

    class Boy
    {
        internal void Action(object sender, ElapsedEventArgs e)
        {
            Console.WriteLine("Jump!");
        }
    }

    class Girl
    {
        internal void Action(object sender, ElapsedEventArgs e)
        {
            Console.WriteLine("Sing!");
        }
    }
}


扫描二维码推送至手机访问。

版权声明:本文由lovedm.club发布,如需转载请注明出处。

本文链接:https://lovedm.club/?id=24

标签: C#事件
分享给朋友:
返回列表

上一篇:C# 委托

下一篇:C#事件_Sample_2

“C#事件_Sample_1” 的相关文章

C# try-catch处理异常

使用try-catch进行异常处理,下面是两个小例子:两个例子中没有写finally语句finally的作用是无论有无异常,finally下的语句都会执行。//简单的处理异常namespace _20200323 {     class ...

C# 委托

C# 委托

Action和Func是.NET类库的内置委托,以简便使用。Func有17个重载还可以使用delegate关键字创建委托下面的代码展示了这三种使用委托的方法namespace _20200327 {     public delegat...

C#事件_Sample_2

事件的拥有者与事件的响应者是分开的情况+=是事件订阅操作符,左边是事件,右边是事件处理器。using System; using System.Windows.Forms; /// <summary> /// 事件的拥有者和事件的响应者是...

C# 泛型委托

C# 泛型委托

虽然没有必要,但是还是先看看自定义的泛型委托:namespace _20200402 {     class Program     {     &nb...

C# Lambda表达式

简单用法,一句一句来,便于理解 Func<int, int, int> func = new Func<int, int, int>((int a, int b) => { return a * b; });(int a, int b) => { ret...

C# 正则表达式(2)

// pattan = @"[^ahou]"; 表示匹配除ahou之外的字符,^在表示反义 string res4 = Regex.Replace(s, @"[^ahou]",&...