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

C#事件_Sample_1

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

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

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” 的相关文章

类、对象、属性、方法

类:具有同种属性的对象称为类,是个抽象的概念。比如说:汽车、人、狗、房子;对象:类实例化后形成对象,具体的概念。如:小明是人的实例化;属性:用来描述具体某个对象的特征的是属性,是静态的。比如:小明身高1.8米多,体重50kg都是属性;方法:每个对象有它们自己的行为或者是使用它们的方法,比如说一只狗会...

C#事件_Sample_3

事件的拥有者同时是事件的响应者using System; using System.Windows.Forms; /// <summary> /// 事件的拥有者同时是事件的响应者 /// </summary> na...

C# 正则表达式(1)

C# 正则表达式(1)

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

C# 正则表达式(2)

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

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

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

PIE二次开发 加载栅格数据

1、获得栅格数据集路径2、打开栅格数据集3、创建栅格图层4、将数据添加到图层并刷新要添加两个引用:using PIE.DataSource;using PIE.Carto;// 获得要打开栅格数据的路径 OpenFileDialog file = new&n...