溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何利用C#實現(xiàn)接口事件

發(fā)布時間:2021-06-16 14:10:22 來源:億速云 閱讀:301 作者:chen 欄目:編程語言

這篇文章主要介紹“如何利用C#實現(xiàn)接口事件”,在日常操作中,相信很多人在如何利用C#實現(xiàn)接口事件問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何利用C#實現(xiàn)接口事件”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

C#接口事件的實現(xiàn)是如何的呢?下面的C#接口事件示例演示如何在類中實現(xiàn)接口事件。實現(xiàn)C#接口事件的規(guī)則與實現(xiàn)任何接口方法或?qū)傩缘囊?guī)則基本相同。

C#接口事件實例:

在類中實現(xiàn)接口事件,在類中聲明事件,然后在適當?shù)膮^(qū)域調(diào)用該事件。

public interface IDrawingObject  {  event EventHandler ShapeChanged;  }  public class MyEventArgs : EventArgs {…}  public class Shape : IDrawingObject  {  event EventHandler ShapeChanged;  void ChangeShape()  {  // Do something before the event…  OnShapeChanged(new MyEventsArgs(…));  // or do something after the event.   }  protected virtual void OnShapeChanged(MyEventArgs e)  {  if(ShapeChanged != null)  {     ShapeChanged(this, e);  }  }  }

C#接口事件示例

下面的示例演示如何處理以下的不常見情況:您的類是從兩個以上的接口繼承的,每個接口都含有同名事件)。在這種情況下,您至少要為其中一個事件提供顯式接口實現(xiàn)。為事件編寫顯式接口實現(xiàn)時,必須編寫 add 和 remove 事件訪問器。這兩個事件訪問器通常由編譯器提供,但在這種情況下編譯器不能提供。

您可以提供自己的訪問器,以便指定這兩個事件是由您的類中的同一事件表示,還是由不同事件表示。例如,根據(jù)接口規(guī)范,如果事件應(yīng)在不同時間引發(fā),則可以將每個事件與類中的一個單獨實現(xiàn)關(guān)聯(lián)。在下面的示例中,訂戶將形狀引用強制轉(zhuǎn)換為 IShape 或 IDrawingObject,從而確定自己將會接收哪個 OnDraw 事件。

C#接口事件代碼:

namespace WrapTwoInterfaceEvents  {  using System;   public interface IDrawingObject  {  // Raise this event before drawing  // the object.  event EventHandler OnDraw;  }  public interface IShape  {  // Raise this event after drawing  // the shape.  event EventHandler OnDraw;  }    // Base class event publisher inherits two  // interfaces, each with an OnDraw event  public class Shape : IDrawingObject, IShape  {  // Create an event for each interface event  event EventHandler PreDrawEvent;  event EventHandler PostDrawEvent;   object objectLock = new Object();   // Explicit interface implementation required.  // Associate IDrawingObject's event with  // PreDrawEvent  event EventHandler IDrawingObject.OnDraw  {  add  {  lock (objectLock)  {  PreDrawEvent += value;  }  }  remove  {  lock (objectLock)  {  PreDrawEvent -= value;  }  }  }  // Explicit interface implementation required.  // Associate IShape's event with  // PostDrawEvent  event EventHandler IShape.OnDraw  {  add   {  lock (objectLock)  {  PostDrawEvent += value;  }  }  remove  {  lock (objectLock)  {  PostDrawEvent -= value;  }  }    }   // For the sake of simplicity this one method  // implements both interfaces.   public void Draw()  {  // Raise IDrawingObject's event before the object is drawn.  EventHandler handler = PreDrawEvent;  if (handler != null)  {  handler(this, new EventArgs());  }  Console.WriteLine("Drawing a shape.");   // RaiseIShape's event after the object is drawn.  handler = PostDrawEvent;  if (handler != null)  {  handler(this, new EventArgs());  }  }  }  public class Subscriber1  {  // References the shape object as an IDrawingObject  public Subscriber1(Shape shape)  {  IDrawingObject d = (IDrawingObject)shape;  d.OnDraw += new EventHandler(d_OnDraw);  }   void d_OnDraw(object sender, EventArgs e)  {  Console.WriteLine("Sub1 receives the IDrawingObject event.");  }  }  // References the shape object as an IShape  public class Subscriber2  {  public Subscriber2(Shape shape)  {  IShape d = (IShape)shape;  d.OnDraw += new EventHandler(d_OnDraw);  }   void d_OnDraw(object sender, EventArgs e)  {  Console.WriteLine("Sub2 receives the IShape event.");  }  }    public class Program  {  static void Main(string[] args)  {  Shape shape = new Shape();  Subscriber1 sub = new Subscriber1(shape);  Subscriber2 sub2 = new Subscriber2(shape);  shape.Draw();   // Keep the console window open in debug mode.  System.Console.WriteLine("Press any key to exit.");  System.Console.ReadKey();  }  }   }
/* C#接口事件示例Output:  Sub1 receives the IDrawingObject event.  Drawing a shape.  Sub2 receives the IShape event.  */

到此,關(guān)于“如何利用C#實現(xiàn)接口事件”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI