C#事件与接口编程实例
很多初学c#的朋友对于事件与接口感到迷惑不解,不明白它们之间的关系,下面我就用实例来简单的分析讲解一下。
事件,用event修饰符来代表一个事件,我们要创建一个C#事件必须按以下顺序来扫行:
1,创建或标识一个代表。比如下例中的public delegate void dele(); //声明代表,delegate 关键字通知编译器 dele 是一个委托类型
2,创建一个包含事件处理代表,调用事件处理代表的方法的类,如下例
public class EventClass1 : IEvents //IEvents,是下面我们要讲一接口
{
public event dele event1;//定义事件成员event1
public void FireEvent() //当事件发生时
{
event1(); //调用事件处理
}
}
EventClass1继承接口IEvents,以下后面的EventClass2~4,都是一样。
3,定义一个或多个把方法连接到事件的类
4,使用事件
4.1 定义事件响应方法,如下例中的
IEvents id1 = new EventClass1();
4.2 使用所定义的构造函数创建一个包含事件的对象,如下例中的
id1.event1 += new dele(EventFired1);
4.3 触发事件,如下例中的
id1.FireEvent();
下面我们来看看接口,我们必须用interface来声明一个接口。接口声明可以声明零个或多个成员。接口的成员必须是方法、属性、事件或索引器。接口不能包含常数、字段、运算符、实例构造函数、析构函数或类型,也不能包含任何种类的静态成员。
所有接口成员都隐式地具有 public 访问权限。接口成员声明包含任何修饰符属于编译时错误。具体地说,接口成员包含下列任何修饰符属于编译时错误:abstract、public、protected、internal、private、virtual、override 或 static。更多的信息请看msdn help://MS.VSCC/MS.MSDNVS.2052/csspec/html/vclrfcsharpspec_13_1.htm
在下面的例子中,我们声明IEvents接口,一个方法FireEvent和一个事件event1
public interface IEvents
{
event dele event1; //定义事件
void FireEvent();//定义接口
}
在后面的EventClass1~4类是继承了接口IEvent,因此在这几个类中必须实现上述一个方法和一个事件。下面的实例可以帮助大家更好的理解。
这是一个简单的windows Forms,包含一个textbox,几个labels和一个button,在程序启动时焦点在textbox,捕捉键盘按下事件,除方向键外,我能过接口来触事方向键按下事件。
下面的代码是一个网上常见的例程,大家可以拷贝下来,保存为.cs文件,用CSC编译就行
代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Events_Interfaces
{
public delegate void dele();//声明代表 delegate 关键字通知编译器 dele 是一个委托类型
public interface IEvents //定义接口IEvents,包含方法FireEvent事件event1
{
event dele event1;
void FireEvent();
}
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label3;
private System.ComponentModel.Container components =null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();
this.textBox1.Location = new System.Drawing.Point(8, 80);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(56,23);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Key_Press);
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(256,64);
this.label1.TabIndex = 0;
this.label1.Text = "Whenever you use the arrow keys inside the text box, Corresponding events will be" +"fired to display the label appropriately. Have a try!!";
this.button1.Location = new System.Drawing.Point(240, 112);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(48,23);
this.button1.TabIndex = 3;
this.button1.Text = "Exit";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(88, 80);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(184,23);
this.label2.TabIndex = 2;
this.label2.TextAlign =System.Drawing.ContentAlignment.MiddleCenter;
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 104);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(64,23);
this.label3.TabIndex = 4;
this.label3.TextAlign =System.Drawing.ContentAlignment.MiddleCenter;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 16);
this.ClientSize = new System.Drawing.Size(292,141);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.label3,this.button1,this.label2,this.textBox1,this.label1});
this.Font= new System.Drawing.Font("Comic SansMS",8.25F,System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point,((System.Byte)(0)));
this.Name = "Form1";
this.Text = "Events";
this.ResumeLayout(false);
}
#endregion
static void Main()
{
Application.Run(new Form1());
}
private void Key_Press(object sender,
System.Windows.Forms.KeyEventArgs e)
{
textBox1.Text = "";
label2.Text = "";
string keyId = e.KeyCode.ToString();
switch (keyId)//判断是否按下方向键
{
case "Right":
label3.Text = "";
IEvents id1 = new EventClass1(); //实例化一个接口
id1.event1 += new dele(EventFired1);//定义EventClass1中的事件响应方法
id1.FireEvent();//调用EventClass1中的FireEvent方法,触发event1 事件,事件调用EventFired1方法
break;
case "Left":
label3.Text = "";
IEvents id2 = new EventClass2();
id2.event1 += new
dele(EventFired2);
id2.FireEvent();
break;
case "Down":
label3.Text = "";
IEvents id3 = new EventClass3();
id3.event1 += new
dele(EventFired3);
id3.FireEvent();
break;
case "Up":
label3.Text = "";
IEvents id4 = new EventClass4();
id4.event1 += new
dele(EventFired4);
id4.FireEvent();
break;
default:
label3.Text = keyId;
break;
}
}
//EventFired1方法
public void EventFired1()
{
label2.Text = "";
label2.Text = "You pressed RIGHT arrow key";
}
public void EventFired2()
{
label2.Text = "";
label2.Text = "You pressed LEFT arrow key";
}
public void EventFired3()
{
label2.Text = "";
label2.Text = "You pressed DOWN arrow key";
}
public void EventFired4()
{
label2.Text = "";
label2.Text = "You pressed UP arrow key";
}
private void button1_Click(object sender,
System.EventArgs e)
{
Application.Exit();
}
}
public class EventClass1 : IEvents
{
public event dele event1;
public void FireEvent()
{
event1();
}
}
public class EventClass2 : IEvents
{
public event dele event1;
public void FireEvent()
{
event1();
}
}
public class EventClass3 : IEvents
{
public event dele event1;
public void FireEvent()
{
event1();
}
}
public class EventClass4 : IEvents//EventClass1继承接口IEvents
{
public event dele event1;//定义事件成员event1
//当事件发生时
public void FireEvent()
{
event1();//调用事件处理
}
}
}
C#事件与接口编程实例的更多相关文章
- yii2 restful api——app接口编程实例
<?php namespace common\components; use common\models\Cart; use common\models\User; use Yii; use y ...
- STM32W108无线射频模块串行通信接口编程实例
STM32W108无线射频模块UART通信应用实例 基于STM32W108芯片,编写串口測试程序,測试串口通信.完毕PC通过串口与STM32W108进行通信. 开发环境与硬件平台 硬件:STM32W1 ...
- C语言与MATLAB接口 编程与实例 李传军编着
罗列一下以前自己学习C语言与MATLAB混编的笔记,顺便复习一遍. <C语言与MATLAB接口 编程与实例 李传军编着>(未看完,目前看到P106) 目录P4-8 ************ ...
- Python并发编程实例教程
有关Python中的并发编程实例,主要是对Threading模块的应用,文中自定义了一个Threading类库. 一.简介 我们将一个正在运行的程序称为进程.每个进程都有它自己的系统状态,包含内存状态 ...
- 【GoLang】golang 面向对象编程 & 面向接口编程
005.面向对象&接口编程 1 面向函数编程 1.1 将数据作为参数传递到函数入参 1.2 对象与函数是分离的 2 面向对象编程 2.1 使用者看起来函数作为对象的属性而非参数 2.2 函数属 ...
- 蓝牙防丢器原理、实现与Android BLE接口编程
本文是对已实现的蓝牙防丢器项目的总结,阐述蓝牙防丢器的原理.实现与android客户端的蓝牙BLE接口编程.在这里重点关注如何利用BLE接口来进行工程实现,对于BLE的协议.涉及到JNI的BLE接口内 ...
- SAP接口编程 之 JCo3.0系列(01):JCoDestination
SAP接口编程 之 JCo3.0系列(01):JCoDestination 字数2101 阅读103 评论0 喜欢0 JCo3.0是Java语言与ABAP语言双向通讯的中间件.与之前1.0/2.0相比 ...
- SAP接口编程 之 JCo3.0系列(02) : JCo Client Programming
SAP接口编程 之 JCo3.0系列(02) : JCo Client Programming 字数545 阅读52 评论0 喜欢1 JCo3.0调用SAP函数的过程 大致可以总结为以下步骤: 连接至 ...
- JAX-RS 2.0 REST客户端编程实例
JAX-RS 2.0 REST客户端编程实例 2014/01/28 | 分类: 基础技术, 教程 | 0 条评论 | 标签: JAX-RS, RESTFUL 分享到:3 本文由 ImportNew - ...
随机推荐
- POJ1036 Gangsters
题目来源:http://poj.org/problem?id=1036 题目大意: 有N个强盗要进入一家饭店打劫,第i个强盗在Ti时刻到达,会抢到Pi的财产.饭店的门有K+1状态,门打开的程度为[0, ...
- iptables端口转发规则(内网端口转外网端口)
需求:外网124.202.173.118需要访问 10.45.225.70的内网54032端口,10.45.225.70服务器有公网地址139.129.109.81将内网地址端口转发到外网地址端口,并 ...
- Django 04 模板标签(if、for、url、with、autoeacape、模板继承于引用、静态文件加载)
Django 04 模板标签(if.for.url.with.autoeacape.模板继承于引用.静态文件加载) 一.if.for.url.with.autoescape urlpatterns = ...
- 转 xshell 图像化展示
http://www.cnblogs.com/kellyseeme/p/7965830.html 限制: 无法显示通过堡垒机登录的机器的图形接界面. 只能显示直接登录的服务到期的图像化界面 Xshel ...
- HDU 4143 A Simple Problem 分解因式
求一个最小的正整数x,使得(y + x) (y - x) = n成立 考虑一下n的分解因式. 可能会想到枚举n的约数,那么a * b = n成立,取最小的x即可 但是要枚举到n / 2,这样会超时. ...
- 解决apache启动错误 AH00558: httpd: Could not reliably determine...
[root@localhost httpd-2.4.7]# /usr/local/httpd/bin/apachectl start AH00558: httpd: Could not reliabl ...
- windows用一键安装包安装(推荐)
为了简化大家在windows下面的安装,我们在xampp基础上做了禅道的windows一键安装包.xampp是业内非常著名的AMP集成运行环境.禅道的一键安装包主要在它基础上做了大量的精简,并集成了我 ...
- Primefaces dataTable设置某个cell的样式问题
设置primefaces dataTable的源网段列的Cell可以编辑,当回车键保存时,判断是否输入的网段合法,如果不合法就显示警告信息,并将这个不合法的数据用红色表示.问题是,怎么给这一个cell ...
- 性能测试学习第十天_controller
集合点设置 controller虚拟多个用户执行脚本启动步骤不一定同步,集合点在脚本的某处设置一个标记,当有虚拟用户运行到这个标记的时候,停下等待所有用户都达到这个标记,再一同进行下面的步骤.这样可以 ...
- redhat配置dns服务器bind
配置Oracle11g的RAC需要使用DNS服务器来解析SCAN IP,本文就是以此为例介绍bind服务器的使用.首先科普一下bind服务器,属于企业级产品了,还是开源的: Bind是Berkeley ...