1. public delegate void TestDelegate(); // delegate declaration
  2.  
  3. public interface ITestInterface
  4. {
  5. event TestDelegate TestEvent;
  6. void FireAway();
  7. }
  8.  
  9. public class TestClass : ITestInterface
  10. {
  11. public event TestDelegate TestEvent;
  12.  
  13. public void FireAway()
  14. {
  15. if (TestEvent != null)
  16. {
  17. TestEvent();
  18. }
  19. }
  20. }
  21.  
  22. public class MainClass
  23. {
  24. static private void F()
  25. {
  26. System.Console.WriteLine("This is called when the event fires.");
  27. }
  28.  
  29. static void Main()
  30. {
  31. ITestInterface i = new TestClass();
  32.  
  33. i.TestEvent += new TestDelegate(F);
  34. i.FireAway();
  35. }
  36. }

2、接口事件、属性事件

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

C#:实现接口中定义的事件的更多相关文章

  1. java 子接口中定义与父接口相同的方法

    今天碰到一个很有意思的问题,在java中如果子接口中定义了与父接口中已经有的方法会发生什么事情呢?比如: interface IRunnable extends Runnable{ void run( ...

  2. 教你在Java接口中定义方法

    基本上所有的Java教程都会告诉我们Java接口的方法都是public.abstract类型的,没有方法体的. 但是在JDK8里面,你是可以突破这个界限的哦. 假设我们现在有一个接口:TimeClie ...

  3. java接口中定义成员变量

    //抽象类中可以定义如下成员变量:public abstract class People { public String name; public int age; public abstract ...

  4. java如何引入接口中定义的常量

    接口 (A.java) : package config; public interface A { String PROJECT_ROOT_DIR = System.getProperty(&quo ...

  5. 接口中定义变量必须为public static final的原因

    在interface里面的变量默认都是public static final 的,原因如下: 1.   接口是一种高度抽象的"模版",,而接口中的属性也就是’模版’的成员,就应当是 ...

  6. mybatis 接口中定义方法、映射文件、实体类之间的关系?

    一.定义实体类 ,注意需求 是一对多还是多对一.  这里用员工和部门  多对一的关系举例. package com.zs.entity; /* * /* * 多对一? * 多个员工 对应一个部门 一个 ...

  7. VB.NET在基类中定义共享事件(类似于C#中的静态事件)

    基类: Public Class userFun Private Shared _PnlStatus As String ‘必须设为共享字段,如果不设为Shared,将不能传递字符串内容 Public ...

  8. js for循环中定义clike事件由于闭包导致的循环变量获取不到的问题

    在网上找的 记下来以备不时之需 案例; 本人有一个数组按钮  循环数组按钮 给每个按钮添加click事件 原本以为搞定但是出现了 每个按钮都是数组最后的方法 然后查找问题 发现onclike事件中的i ...

  9. Java 接口中定义抽象方法有什么意义

    接口方法声明只能是public abstract的,所以不管你在声明的时候加不加abstract,都是可以的.Java 8开始,接口还引入了默认方法,也就是可以给接口的方法提供默认的实现,默认方法应当 ...

随机推荐

  1. TXT导入出现乱码

    错误#1 11:15 2012-12-19客户提供一txt文本文件,要求导入到数据库,选用dts导入工具,选择数据源步骤如下列预览时出现乱码解答#1 双击打开原始文件中文显示正常,将其另存为选择编码为 ...

  2. python_文件

    1. 打开文件 (1) open(name[, mode[, buffering]]) 功能:打开文件或者新建一个文件 参数说明: mode: "r" : 读模式(默认)   &q ...

  3. 关于开源授权协议 GPL 和 LGPL

    GPL 是 GNU General Public License (GNU 通用公共许可证)的缩写形式:LGPL 是 GNU Lesser General Public License (GNU 宽通 ...

  4. swift 同步加载图片

    import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: ...

  5. Java基础之创建窗口——使用GridBagLayout管理器(TryGridBagLayout)

    控制台程序. java.awt.GridBagLayout管理器比前面介绍的其他布局管理器灵活得多,因此使用起来也比较复杂.基本机制就是在随意的矩形网格中布局组件,但网格的行和列不一定拥有相同的高度和 ...

  6. linux:磁盘的分割、检验、格式化与挂载

    新增一颗磁碟: 1.对磁碟进行分割,以建立可用的partition 2.对该分割槽partition进行格式化(format),以建立系统可用的filesystem 3.若要仔细点,可对刚刚建立的fi ...

  7. Leetcode: Nth Digit

    Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...

  8. zabbix监控企业esxi虚拟机

    zabbix监控企业esxi虚拟机 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我来公司有一段时间了,最近也发现模型部有测试和开发反应某台机器登陆不上去了,结果登陆esxi服务器 ...

  9. java类的定义以及参数传递

    class类(类似结构体)的定义 import java.util.Scanner; import java.io.*; class student{//类的名称 public String name ...

  10. .net开发,html ajax开发架构之我见 bs ajax最简化法 Knock out Request,totally oo

    .net开发中,无论ajax还是webform,webpage, 总免不了要和request这个静态全局,可以远程通信的对象打交道. 而对于软件来讲,按照Matin Fowler的的面向对象,可利用软 ...