public delegate void TestDelegate();   // delegate declaration

 public interface ITestInterface
{
event TestDelegate TestEvent;
void FireAway();
} public class TestClass : ITestInterface
{
public event TestDelegate TestEvent; public void FireAway()
{
if (TestEvent != null)
{
TestEvent();
}
}
} public class MainClass
{
static private void F()
{
System.Console.WriteLine("This is called when the event fires.");
} static void Main()
{
ITestInterface i = new TestClass(); i.TestEvent += new TestDelegate(F);
i.FireAway();
}
}

2、接口事件、属性事件

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();
}
} }
/* Output:
Sub1 receives the IDrawingObject event.
Drawing a shape.
Sub2 receives the IShape event.
*/

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. Python模块(getpass)

    getpass getpass模块用于输入信息时不显示,比如输入密码时隐藏.getpass模块接收用户的输入的数据类型是str类型. #!/usr/bin/env python #-*- coding ...

  2. App上架出现问题-ERROR ITMS-90034

    1.打开钥匙串,显示->显示已过期的证书,因为有些过期的证书会被钥匙串隐藏起来,让过期的证书都显示出来,删掉过期的证书. 2.经检查,我发现我的AppleWWDRCA.cer证书过期了,所以我就 ...

  3. subprocess使用

    1. Popen使用 test = subprocess.Popen('ls /tmpa', shell=True, stdout = subprocess.PIPE, stderr=subproce ...

  4. [转]那些年我还不懂:IList,ICollection,IEnumerable,IEnumerator,IQueryable

    1.首先看一个简单的例子 int[] myArray = { 1, 32, 43, 343 }; IEnumerator myie = myArray.GetEnumerator(); myie.Re ...

  5. javax.xml.ws.webserviceexception class do not have a property of the name

    我是用wsimport生成webservice 的客户端,放到工程里,调用,出现这个异常, 后来发现,是没有把package-info.java这个文件一起放到包里的缘故 解决: 连同package- ...

  6. iptables使用

    iptables规则的查看.添加.删除和修改 1.查看 iptables -nvL --line-number (这个命令跟/etc/init.d/iptables status 输出差不多) -L ...

  7. FB面经prepare: Task Schedule

    每种task都有冷却时间,比如task1执行后,要经过interval时间后才能再次执行,求总共所需时间. 用HashMap保存每一个task的下一次可以开始执行的最早时间 package TaskS ...

  8. IOS Suppot Font 苹果默认支持的字体一览(配图)

    这些字体都是IOS设备(使用ipad2测试) 默认支持的字体,也就是在AIR中不用设置绑定字体情况下 看到的样子 感觉上应该IOS仅为中文设置了一种字体就是 Heiti SC

  9. CSS自定义弹出框

    <script type="text/javascript" language="javascript"> function sAlert(str) ...

  10. Python学习总结19:类(二)

    参考:http://python.jobbole.com/82308/ 继承和__slots__属性 1. 继承    在Python中,同时支持单继承与多继承,一般语法如下: class SubCl ...