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

C#事件_Sample_1

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

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

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# 泛型委托

C# 泛型委托

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

C# 正则表达式(1)

C# 正则表达式(1)

用于匹配输入文本的模式string s = "this is a test!"; string res = Regex.Replace(s, "^",&nbs...

C#(或者Java)反转数组

将原数组反转,如[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]反转后变为[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]因为数组是引用类型,所以直接在方法中处理即可,C#和Java写法一样,如下:      &nb...

C语言malloc()函数

C语言中malloc()函数,用于分配所需的内存,并返回一个指向该内存的指针。注意这是C的标准库函数,不是C的关键字,在<stdlib.h>头文件下。函数声明: void *malloc(size_t size)其中,size是要分配的内存的大小,单位是字节。返回一个指针 ,指向已分配大...

C语言qsort简单使用

qsort函数位于stdlib.h头文件里。是用来排序的函数,函数原型如下:void qsort(void *ptr, size_t count, size_t size, int (*comp)(const void *, const void *));四个参数:ptr:指向待排序数组的指针cou...