成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C#接口事件的實(shí)現(xiàn)解析

開發(fā) 后端
C#接口事件的實(shí)現(xiàn)是如何辦到的呢?C#接口事件的實(shí)現(xiàn)具體的步驟和需要注意的事項(xiàng),那么本文就向你介紹具體的內(nèi)容。

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

C#接口事件實(shí)例:

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

  1. public interface IDrawingObject  
  2. {  
  3. event EventHandler ShapeChanged;  
  4. }  
  5. public class MyEventArgs : EventArgs {…}  
  6. public class Shape : IDrawingObject  
  7. {  
  8. event EventHandler ShapeChanged;  
  9. void ChangeShape()  
  10. {  
  11. // Do something before the event…  
  12. OnShapeChanged(new MyEventsArgs(…));  
  13. // or do something after the event.   
  14. }  
  15. protected virtual void OnShapeChanged(MyEventArgs e)  
  16. {  
  17. if(ShapeChanged != null)  
  18. {  
  19.    ShapeChanged(this, e);  
  20. }  
  21. }  

C#接口事件示例

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

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

C#接口事件代碼:

  1. namespace WrapTwoInterfaceEvents  
  2. {  
  3. using System;  
  4.  
  5. public interface IDrawingObject  
  6. {  
  7. // Raise this event before drawing  
  8. // the object.  
  9. event EventHandler OnDraw;  
  10. }  
  11. public interface IShape  
  12. {  
  13. // Raise this event after drawing  
  14. // the shape.  
  15. event EventHandler OnDraw;  
  16. }  
  17.  
  18.  
  19. // Base class event publisher inherits two  
  20. // interfaces, each with an OnDraw event  
  21. public class Shape : IDrawingObject, IShape  
  22. {  
  23. // Create an event for each interface event  
  24. event EventHandler PreDrawEvent;  
  25. event EventHandler PostDrawEvent;  
  26.  
  27. object objectLock = new Object();  
  28.  
  29. // Explicit interface implementation required.  
  30. // Associate IDrawingObject's event with  
  31. // PreDrawEvent  
  32. event EventHandler IDrawingObject.OnDraw  
  33. {  
  34. add  
  35. {  
  36. lock (objectLock)  
  37. {  
  38. PreDrawEvent += value;  
  39. }  
  40. }  
  41. remove  
  42. {  
  43. lock (objectLock)  
  44. {  
  45. PreDrawEvent -= value;  
  46. }  
  47. }  
  48. }  
  49. // Explicit interface implementation required.  
  50. // Associate IShape's event with  
  51. // PostDrawEvent  
  52. event EventHandler IShape.OnDraw  
  53. {  
  54. add   
  55. {  
  56. lock (objectLock)  
  57. {  
  58. PostDrawEvent += value;  
  59. }  
  60. }  
  61. remove  
  62. {  
  63. lock (objectLock)  
  64. {  
  65. PostDrawEvent -= value;  
  66. }  
  67. }  
  68.  
  69.  
  70. }  
  71.  
  72. // For the sake of simplicity this one method  
  73. // implements both interfaces.   
  74. public void Draw()  
  75. {  
  76. // Raise IDrawingObject's event before the object is drawn.  
  77. EventHandler handler = PreDrawEvent;  
  78. if (handler != null)  
  79. {  
  80. handler(thisnew EventArgs());  
  81. }  
  82. Console.WriteLine("Drawing a shape.");  
  83.  
  84. // RaiseIShape's event after the object is drawn.  
  85. handler = PostDrawEvent;  
  86. if (handler != null)  
  87. {  
  88. handler(thisnew EventArgs());  
  89. }  
  90. }  
  91. }  
  92. public class Subscriber1  
  93. {  
  94. // References the shape object as an IDrawingObject  
  95. public Subscriber1(Shape shape)  
  96. {  
  97. IDrawingObject d = (IDrawingObject)shape;  
  98. d.OnDraw += new EventHandler(d_OnDraw);  
  99. }  
  100.  
  101. void d_OnDraw(object sender, EventArgs e)  
  102. {  
  103. Console.WriteLine("Sub1 receives the IDrawingObject event.");  
  104. }  
  105. }  
  106. // References the shape object as an IShape  
  107. public class Subscriber2  
  108. {  
  109. public Subscriber2(Shape shape)  
  110. {  
  111. IShape d = (IShape)shape;  
  112. d.OnDraw += new EventHandler(d_OnDraw);  
  113. }  
  114.  
  115. void d_OnDraw(object sender, EventArgs e)  
  116. {  
  117. Console.WriteLine("Sub2 receives the IShape event.");  
  118. }  
  119. }  
  120.  
  121.  
  122. public class Program  
  123. {  
  124. static void Main(string[] args)  
  125. {  
  126. Shape shape = new Shape();  
  127. Subscriber1 sub = new Subscriber1(shape);  
  128. Subscriber2 sub2 = new Subscriber2(shape);  
  129. shape.Draw();  
  130.  
  131. // Keep the console window open in debug mode.  
  132. System.Console.WriteLine("Press any key to exit.");  
  133. System.Console.ReadKey();  
  134. }  
  135. }  
  136.  
  1. /* C#接口事件示例Output:  
  2. Sub1 receives the IDrawingObject event.  
  3. Drawing a shape.  
  4. Sub2 receives the IShape event.  
  5. */ 

C#接口事件的實(shí)現(xiàn)以及使用的一些內(nèi)容就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#接口事件有所幫助。

【編輯推薦】

  1. C#接口的定義詳解
  2. C#接口編程之接口成員淺析
  3. C#實(shí)現(xiàn)接口的實(shí)例解析
  4. C#接口的作用實(shí)例解析
  5. C#接口實(shí)例應(yīng)用的的深入探討
責(zé)任編輯:仲衡 來(lái)源: CSDN
相關(guān)推薦

2009-08-31 17:16:12

C#實(shí)現(xiàn)接口

2009-08-31 17:47:43

C#接口使用

2009-08-27 17:40:21

C#接口的作用

2009-08-31 17:30:10

C#接口的作用

2009-08-31 18:17:32

C#接口編程

2009-08-25 17:55:52

C#實(shí)現(xiàn)Strateg

2009-08-31 15:55:17

C#實(shí)現(xiàn)Strateg

2011-08-23 17:11:13

Lua事件C#

2009-09-02 16:30:20

C#定義數(shù)組

2009-09-07 15:27:04

C# MessageB

2009-08-24 10:06:31

C#接口成員

2009-09-03 16:38:49

C#回車鍵事件

2009-08-24 10:47:45

C#接口重實(shí)現(xiàn)

2009-08-31 16:48:02

C#實(shí)現(xiàn)IDispos

2009-08-31 16:23:13

C#接口

2009-09-04 13:22:31

C#實(shí)現(xiàn)多個(gè)接口

2009-09-01 18:29:24

C#實(shí)現(xiàn)多個(gè)接口

2009-09-09 13:18:10

C# TextBox滾C# TextBox

2009-09-09 11:29:32

C# TextBox事

2009-08-18 10:48:25

C#事件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 欧美久久久电影 | 青青草在线播放 | 成人永久免费视频 | 亚洲精品片 | 中文字幕在线国产 | 久久久久久国产精品免费免费 | 久久69精品久久久久久久电影好 | 成人午夜激情 | 日韩成人免费视频 | 欧美一区二区三区在线 | 国产精品久久国产精品 | 国产精品69毛片高清亚洲 | 久久一及片 | 国产一区二区视频免费在线观看 | 亚洲欧美精品一区 | 日本欧美在线观看视频 | 国产一区二区不卡 | 羞羞的视频在线看 | 狠狠视频| 日韩视频在线免费观看 | 日本精品久久 | 亚洲综合网站 | 久久精品网 | 草久久免费视频 | 精品久久香蕉国产线看观看亚洲 | 中文字幕在线观看一区二区 | 九九在线| www九色| 91视频一区二区三区 | 激情国产 | 三级视频在线观看 | 日本a视频| 人人干人人超 | 亚洲一区二区三区在线视频 | 日韩精品成人免费观看视频 | 午夜视频在线 | 国产91av视频在线观看 | 午夜小电影 | 亚洲成人中文字幕 | 亚洲毛片 | 呦呦在线视频 |