C#基础委托之重调方法

2021-01-05 12:29

阅读:1457

标签: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 func = new Func(productFactory.MakePizza);
            Func func1 = new Func(productFactory.MakeToyCar);

            Logger logger = new Logger();

            Action action = new Action(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 getProduct,Action 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


评论


亲,登录后才可以留言!