C#基础难委托之模板方法

2021-01-05 09:29

阅读:585

标签:ace   make   name   简单   color   ret   new   factory   ogr   

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);

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

            Console.WriteLine(box.Pro.Name);
            Console.WriteLine(box1.Pro.Name);
        }
        class Product
        {
            public string Name { get; set; }
        }
        class Box
        {
            public Product Pro { get; set; }
        }
        class WrapFation
        {
            public Box WrapProduct(Func getProduct)
            {
                Box box = new Box();
                Product product = getProduct.Invoke();
                box.Pro = product;
                return box;
            }
        }
        class ProductFactory
        {
            public Product MakePizza()
            {
                Product product = new Product();
                product.Name = "Pizza";
                return product;
            }
            public Product MakeToyCar()
            {
                Product product = new Product();
                product.Name = "Toy Car";
                return product;
            }
        }
        
    }  
}

一个简单的委托

C#基础难委托之模板方法

标签:ace   make   name   简单   color   ret   new   factory   ogr   

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

上一篇:C# 队列(Queue)

下一篇:java学习心得


评论


亲,登录后才可以留言!