zoukankan      html  css  js  c++  java
  • 一个C#中event的应用实例,对比说明与delegate的区别

    可以看出,C#中的event是通过delegate实现的,event 只是在delegate基础上作了两点限制:
    1:客户只能使用委托所提供的+=和-=行为,不能直接用=,即不影响委托对其他observer的notify.
    2:只有声明类可以调用(或激发)一个事件,外部类不可以直接调用其事件。
    在一个C#接口中可以包容方法、C#属性以及索引器,也可以包容事件,但不能包含委托,因为接口定义的是成员,委托定义的是一个类型,而事件同样是成员。
    using System;

    namespace EventKeyWord
    {
        
    /// <summary>
        
    /// Summary description for Class1.
        
    /// </summary>

        class test
        
    {
            
    /// <summary>
            
    /// The main entry point for the application.
            
    /// </summary>

            [STAThread]
            
    static void Main(string[] args)
            
    {
                
    //
                
    // TODO: Add code to start application here
                
    //
                Clock clk=new Clock();
                DisplayClock dc
    =new DisplayClock();
                dc.subscribe(clk);            
                logClock mlogClock
    =new logClock();
                mlogClock.subscribe(clk);
                clk.run();
                
                
    //if no event in line 45,the OnSecondChange could be invoked directly here
            }

        }

        
    public class TimeInfoEventArgs:EventArgs
        
    {
            
    public readonly int hour;
            
    public readonly int minute;
            
    public readonly int second;
            
    public  TimeInfoEventArgs(int hour,int minute,int second)
            
    {
                
    this.hour=hour;
                
    this.minute=minute;
                
    this.second=second;
            }

        }

        
    public class Clock
        
    {
            
    public delegate void SecondChange(object Clock,TimeInfoEventArgs args);
            
    private int hour;
            
    private int minute;
            
    private int second;
            
    public event SecondChange OnSecondChange;//the keyword event is optional
            public Clock()
            
    {
                hour
    =System.DateTime.Now.Hour;
                minute
    =System.DateTime.Now.Minute;
                second
    =System.DateTime.Now.Second;
            }

            
    public void run()
            
    {
                
    while(1==1)
                
    {
                    System.Threading.Thread.Sleep(
    10);
                    DateTime dt
    =System.DateTime.Now;
                    
    if(dt.Second!=second)
                        
    if(OnSecondChange!=null)
                        
    {
                            OnSecondChange(
    this,new TimeInfoEventArgs(hour,minute,dt.Second));
                        }

                    hour
    =System.DateTime.Now.Hour;
                    minute
    =System.DateTime.Now.Minute;
                    second
    =System.DateTime.Now.Second;

                }

            }

        }

        
    //a class to display the current clock
        public class DisplayClock
        
    {
            
    public void subscribe(Clock clk)
            
    {
                clk.OnSecondChange
    +=new Clock.SecondChange(this.displayClock);            
            }

            
    public void displayClock(object Clock,TimeInfoEventArgs args)
            
    {
                Console.WriteLine(
    "displayClock now:");
                Console.WriteLine(args.hour
    +":"+args.minute+":"+args.second);
            }

        }

        
    //a second class to log the current clock
        public class logClock
        
    {
            
    public void subscribe(Clock clk)
            
    {
                clk.OnSecondChange
    +=new Clock.SecondChange(this.logsClock);
                
    //if no event in line 45,this could be clk.OnSecondChange+=new Clock.SecondChange(this.logsClock); and the displayClock won't work
            }

            
    public void logsClock(object Clock,TimeInfoEventArgs args)
            
    {
                Console.WriteLine(
    "logClock now:");
                Console.WriteLine(args.hour
    +":"+args.minute+":"+args.second);
            }

        }

    }

  • 相关阅读:
    第8周课下作业1(补)
    第八章课下测试
    POJ
    POJ
    HDU
    UVa
    UVa
    CodeForces
    ZOJ
    LightOJ
  • 原文地址:https://www.cnblogs.com/Finding2013/p/464944.html
Copyright © 2011-2022 走看看