码迷,mamicode.com
首页 > Windows程序 > 详细

C#基础委托之重调方法

时间:2020-06-24 00:49:06      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:product   datetime   return   ctf   lin   delegate   wrap   new   log   

using System;
namespace ConsoleTese
{
    class Program
    {
        public delegate int dele(int a, int b);
        static void Main(string[] args)
        {
            ProductFactory productFactory = new ProductFactory();
            WrapFation wrapFation = new WrapFation();

            Func<Product> func = new Func<Product>(productFactory.MakePizza);
            Func<Product> func1 = new Func<Product>(productFactory.MakeToyCar);

            Logger logger = new Logger();

            Action<Product> action = new Action<Product>(logger.Log);

            Box box= wrapFation.WrapProduct(func, action);
            Box box1=wrapFation.WrapProduct(func1, action);

            Console.WriteLine(box.Pro.Name);
            Console.WriteLine(box1.Pro.Name);
        }
        class Logger 
        {
            public void Log(Product product) 
            {
                Console.WriteLine("产品名称:{0},发生时间:{1},价格是:{2}",product.Name,DateTime.UtcNow,product.Proce);
            }    
        }
        class Product
        {
            public string Name { get; set; }
            public double Proce { get; set; }
        }
        class Box
        {
            public Product Pro { get; set; }
        }
        class WrapFation
        {
            public Box WrapProduct(Func<Product> getProduct,Action<Product> action)
            {
                Box box = new Box();
                Product product = getProduct.Invoke();
                if (product.Proce>=50)
                {
                    action(product);
                }
                box.Pro = product;
                return box;
            }
        }
        class ProductFactory
        {
            public Product MakePizza()
            {
                Product product = new Product();
                product.Name = "Pizza";
                product.Proce = 100.00;
                return product;
            }
            public Product MakeToyCar()
            {
                Product product = new Product();
                product.Name = "Toy Car";
                product.Proce = 12.00;
                return product;
            }
        }
        
    }  
}

 

C#基础委托之重调方法

标签:product   datetime   return   ctf   lin   delegate   wrap   new   log   

原文地址:https://www.cnblogs.com/zxbls/p/13185324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!