zoukankan      html  css  js  c++  java
  • C# 委托与事件简单应用

    C# 事件简单应用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace TestIO
    {
        public delegate double PriceHandler();
    
        public class PriceManager
        {
            public PriceHandler GetPriceHandler;
    
            //委托处理,当价格高于100元按8.8折计算,其他按原价计算 
            public double GetPrice()
            {
                //GetPriceHandler.GetInvocationList(); 按照此调用顺序,返回多路广播委托的调用列表
                if (GetPriceHandler.GetInvocationList().Count() > 0)
                {
                    if (GetPriceHandler() > 100)
                        return GetPriceHandler() * 0.88;
                    else
                        return GetPriceHandler();
                }
                return -1;
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                PriceManager priceManager = new PriceManager();
    
                //调用priceManager的GetPrice方法获取价格 
                //直接调用委托的Invoke获取价格,两者进行比较 
    
                priceManager.GetPriceHandler = new PriceHandler(ComputerPrice);
                Console.WriteLine(string.Format("GetPrice\n  Computer's price is {0}!",
                    priceManager.GetPrice()));
                //priceManager.GetPrice();
                Console.WriteLine(string.Format("Invoke\n  Computer's price is {0}!",
                    priceManager.GetPriceHandler.Invoke()));
    
                Console.WriteLine();
    
                priceManager.GetPriceHandler = new PriceHandler(BookPrice);
                Console.WriteLine(string.Format("GetPrice\n  Book's price is {0}!",
                    priceManager.GetPrice()));
                Console.WriteLine(string.Format("Invoke\n  Book's price is {0}!",
                    priceManager.GetPriceHandler.Invoke()));
    
                Console.ReadKey();
            }
            //书本价格为98元 
            public static double BookPrice()
            {
                return 98.0;
            }
            //计算机价格为8800元 
            public static double ComputerPrice()
            {
                return 8800.0;
            }
        } 
    
    
    
    
    }
    


     

    该代码来自cnblogs  http://www.cnblogs.com/philzhou/archive/2012/04/05/2433663.html
  • 相关阅读:
    Ubuntu安装php7.0环境
    PHP-FPM参数详情
    phpize是干嘛的
    Ubuntu忘记密码
    Ubuntu下面删除和卸载软件
    Js验证正则表达式
    JS发送验证码;并设置cookie
    Shell脚本之sed的使用
    Bash基本功能:输入输出重定向
    shell常用快捷键
  • 原文地址:https://www.cnblogs.com/voidobject/p/3975495.html
Copyright © 2011-2022 走看看