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
    我来自:向东博客
  • 相关阅读:
    js --- for in 和 for of
    vue -- config index.js 配置文件详解
    vue -- 脚手架之webpack.dev.conf.js
    vue --- 解读vue的中webpack.base.config.js
    vue 引入第三方字体包
    js call 和 apply
    vue2.0中的$router 和 $route的区别
    懒加载js实现和优化
    vue的指令在webstrom下报错
    BFC的布局规则和触发条件
  • 原文地址:https://www.cnblogs.com/meil/p/435660.html
Copyright © 2011-2022 走看看