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

C#事件_Sample_1

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

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

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

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

C# 泛型委托

C# 泛型委托

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

C# 与文件相关的几个类(1)

C# 与文件相关的几个类(1)

C# 与文件访问相关的常用的类:File类、Directory类、Path类、FileInfo类、DirectoryInfo类、FileSystemInfo类、FileSystemWatcher类以上几个类均在System.IO命名空间下。挨个说吧:File类:静态类,只有静态方法,用于移...

C# 与文件相关的几个类(3)

Path类,处理文件或路径的类,是一个静态类。方法:PathChangeExtension(String, String)更改路径字符串的扩展名。返回值为string。Combine(String, String)将两个字符串组合成一个路径。GetDirectoryName(String)返回指定路...

C# Stack堆栈

Stack代表了一个先入后出的对象集合。有以下常用方法:表 3Clear()从 Stack 中移除所有对象。Contains(Object)确定某元素是否在 Stack 中。CopyTo(Array, Int32)从指定的数组索引处开始,将 Stac...