zoukankan      html  css  js  c++  java
  • 一个C#实现的最简单的委托例子

    using System;

    namespace DelegateSample
    {
        
    public delegate void PrintCallback(int number);

        
    public class Printer{

            
    //委托定义
            private PrintCallback _print;

            
    //委托将要依附的属性
            public PrintCallback PrintCallback{
                
    get{return _print;}
                
    set{_print=value;}
            }

        }


        
    public class Driver{

            
    //将要委托的事件
            private void PrintInteger(int number){
                Console.WriteLine(
    "From PrintInteger:The number is {0}.", number);
            }


            
    static void Main(string[] args){
                Driver driver 
    = new Driver();
                Printer printer
    =new Printer();

                
    //将委托绑定到属性
                printer.PrintCallback = new PrintCallback(driver.PrintInteger);

                
    //使用属性触发委托事件
                printer.PrintCallback(10);
                printer.PrintCallback(
    100);

                Console.WriteLine(
    "press Enter to exit");
                Console.ReadLine();
            }

        }


    }


    结果:
    From PrintInteger:The number 
    is 10.
    From PrintInteger:The number 
    is 100.
    press Enter to exit
    我来自:向东博客
  • 相关阅读:
    淘宝放大镜
    碰撞的小球
    模拟微博发布
    CSS兼容IE Firefox问题与解决方法
    太阳八大行星运行轨迹
    Canvas标签基础
    offsetTop、clientTop、scrollTop、offsetTop
    js绘制圆形时钟
    js时钟
    js五星好评2
  • 原文地址:https://www.cnblogs.com/meil/p/435660.html
Copyright © 2011-2022 走看看