zoukankan      html  css  js  c++  java
  • C#:实现接口中定义的事件

    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();
         }
     }
    View Code

     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.
    */
    View Code
  • 相关阅读:
    Nginx负载均衡和LVS负载均衡的比较分析
    Nginx和Squid配合搭建的Web服务器前端系统
    (总结)Linux服务器上最简单的Nginx反向代理配置
    Nginx主要模块常用指令说明
    (总结)Nginx 502 Bad Gateway错误触发条件与解决方法
    (总结)Linux下查看Nginx Apache MySQL的并发连接数和连接状态
    (总结)统计Apache或Nginx访问日志里的独立IP访问数量的Shell
    IoC模式(依赖、依赖倒置、依赖注入、控制反转)
    使用OAuth打造webapi认证服务供自己的客户端使用
    RESTful API 设计指南
  • 原文地址:https://www.cnblogs.com/shenchao/p/4674264.html
Copyright © 2011-2022 走看看