C#接口编程实例解析

C#接口编程是我们实际开发中经常要处理的目标,那么C#接口编程需要注意什么呢?这里向你介绍的内容是C
首页 新闻资讯 行业资讯 C#接口编程实例解析

C#接口编程实例向你讲述了事件与接口的联合应用很多初学C#的朋友对于事件与接口感到迷惑不解,不明白它们之间的关系,下面我就用实例来简单的分析讲解一下。
 
C#接口编程实例问题的解决之前我们来看看事件的创建过程,用event修饰符来代表一个事件,我们要创建一个C#事件必须按以下顺序来扫行:

C#接口编程实例1,创建或标识一个代表。

比如下例中的

复制

public delegate void dele();   //声明代表,delegate 关键字通知编译器dele是一个委托类型
  • 1.

  • 2.

C#接口编程实例2,创建一个包含事件处理代表,调用事件处理代表的方法的类

复制

public class EventClass1 : IEvents   //IEvents,是下面我们要讲一接口  {  public event dele event1;//定义事件成员event1  public void FireEvent() //当事件发生时  {  event1(); //调用事件处理  }  }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

EventClass1继承接口IEvents,以下后面的EventClass2~4,都是一样。

C#接口编程实例3,定义一个或多个把方法连接到事件的类

C#接口编程实例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();//定义接口  }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

在后面的EventClass1~4类是继承了接口IEvent,因此在这几个类中必须实现上述一个方法和一个事件。下面的实例可以帮助大家更好的理解。

这是一个简单的windows Forms,包含一个textbox,几个labels和一个button,在程序启动时焦点在textbox,捕捉键盘按下事件,除方向键外,我能过接口来触事方向键按下事件。#p#

下面的代码是一个网上常见的C#接口编程实例,大家可以拷贝下来,保存为.cs文件,用CSC编译就行

C#接口编程实例代码如下:

复制

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  //C#接口编程实例  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  //C#接口编程实例  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";  }  //C#接口编程实例  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#接口编程实例之调用事件处理  }  }   }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

  • 28.

  • 29.

  • 30.

  • 31.

  • 32.

  • 33.

  • 34.

  • 35.

  • 36.

  • 37.

  • 38.

  • 39.

  • 40.

  • 41.

  • 42.

  • 43.

  • 44.

  • 45.

  • 46.

  • 47.

  • 48.

  • 49.

  • 50.

  • 51.

  • 52.

  • 53.

  • 54.

  • 55.

  • 56.

  • 57.

  • 58.

  • 59.

  • 60.

  • 61.

  • 62.

  • 63.

  • 64.

  • 65.

  • 66.

  • 67.

  • 68.

  • 69.

  • 70.

  • 71.

  • 72.

  • 73.

  • 74.

  • 75.

  • 76.

  • 77.

  • 78.

  • 79.

  • 80.

  • 81.

  • 82.

  • 83.

  • 84.

  • 85.

  • 86.

  • 87.

  • 88.

  • 89.

  • 90.

  • 91.

  • 92.

  • 93.

  • 94.

  • 95.

  • 96.

  • 97.

  • 98.

  • 99.

  • 100.

  • 101.

  • 102.

  • 103.

  • 104.

  • 105.

  • 106.

  • 107.

  • 108.

  • 109.

  • 110.

  • 111.

  • 112.

  • 113.

  • 114.

  • 115.

  • 116.

  • 117.

  • 118.

  • 119.

  • 120.

  • 121.

  • 122.

  • 123.

  • 124.

  • 125.

  • 126.

  • 127.

  • 128.

  • 129.

  • 130.

  • 131.

  • 132.

  • 133.

  • 134.

  • 135.

  • 136.

  • 137.

  • 138.

  • 139.

  • 140.

  • 141.

  • 142.

  • 143.

  • 144.

  • 145.

  • 146.

  • 147.

  • 148.

  • 149.

  • 150.

  • 151.

  • 152.

  • 153.

  • 154.

  • 155.

  • 156.

  • 157.

  • 158.

  • 159.

  • 160.

  • 161.

  • 162.

  • 163.

  • 164.

  • 165.

  • 166.

  • 167.

  • 168.

  • 169.

  • 170.

  • 171.

  • 172.

  • 173.

  • 174.

  • 175.

  • 176.

  • 177.

  • 178.

  • 179.

  • 180.

  • 181.

  • 182.

  • 183.

  • 184.

  • 185.

  • 186.

  • 187.

  • 188.

  • 189.

  • 190.

  • 191.

  • 192.

  • 193.

  • 194.

  • 195.

  • 196.

  • 197.

  • 198.

  • 199.

  • 200.

  • 201.

  • 202.

  • 203.

  • 204.

  • 205.

  • 206.

  • 207.

  • 208.

  • 209.

  • 210.

  • 211.

  • 212.

  • 213.

  • 214.

  • 215.

  • 216.

  • 217.

  • 218.

  • 219.

  • 220.

  • 221.

  • 222.

  • 223.

  • 224.

  • 225.

  • 226.

  • 227.

C#接口编程实例的基本内容就向你介绍到这里,希望对你了解和学习C#接口编程有所帮助。

【编辑推荐】

  1. C#接口编程之接口成员浅析

  2. C#实现接口的实例解析

  3. C#接口的作用实例解析

  4. C#接口实例应用的的深入探讨

  5. C#接口事件的实现解析

12    2009-08-31 18:17:32    C#接口编程