C# 委托学习笔记

委托是指动态(间接)调用一个分装好的方法体
委托是一种类
两种形式:1.一种是调用没有返回值的方法体,2.另一种的调用有返回值得方法体。

1.调用没有返回值的方法体《Action》

class Program
    {
        static void Main(string[] args)
        {
            Student stu = new Student();//这里还是需要进行对象实例化
            System.Action action = new System.Action(stu.age);//使用(委托)Actin方法调用
            action.Invoke();//调用,此处Invoke可以省略为action(),相当于指针。
        }
    }

    class Student
    {
        public void age()
        {
            Console.WriteLine("age is 18");
        }
    }

2.调用有返回值的方法体《Func》

class Program
    {
        static void Main(string[] args)
        {
            Student stu = new Student();
            Func<int, int, int> fun = new Func<int, int, int>(stu.add);
            //Func<int, int, int>前两个指输入参数int类型,最后一个是返回值的类型
            //括号里面是写方法(stu.add)。也可以写成Student.add,这样就不用创建实列了
            int a = 1;
            int b = 2;
            int c= 0;
            c=fun.Invoke(a, b); //此处Invoke可以省略为action(),相当于c中的指针。
            Console.WriteLine(c);
        }
    }

    class Student
    {
        public  int add(int a, int b)
        {
            return a + b;
        }
    }

3.自定义声明委托与调用《delegate》

public delegate int Cal(int a, int b);
//放在名称空间里,和Program同一级别
        //public公共声明,外部可以访问。delegate我要声明一个委托。int目标返回值类型。 Cal委托名称
class Program
    {
        static void Main(string[] args)
        {
            Student stu = new Student();
            int a=1;
            int b = 2;
            int c = 0;
            Cal cal1 = new Cal(stu.add);//委托创建实列
            Cal cal2 = new Cal(stu.sub);//委托创建实列
            c = cal1(a, b);
            Console.WriteLine(c);
            c = cal2(a, b);
            Console.WriteLine(c);
        }
    }

    class Student
    {
        public int add(int a, int b)
        {
            return a + b;
        }
        public int sub(int a, int b)
        {
            return a - b;
        }
    }

4.委托运用实列

class Program
    {
        static void Main(string[] args)
        {
            ProductFactory productFactory = new ProductFactory();//创建工厂的实列
            WrapFactor wrapFactor = new WrapFactor();//包装的实列
            Func<Product> func1 = new Func<Product>(productFactory.MakePizza);//创建生产披萨的实列
            Func<Product> func2 = new Func<Product>(productFactory.MakeToCar);//创建生产小汽车的实列

            Box box1= wrapFactor.WraapProduct(func1);
            Box box2 = wrapFactor.WraapProduct(func2);

            Console.WriteLine(box1.Product.Name);
        }
    }
    class Product//声明Product类,任何产品都有自己名字
    {
        public string Name { get; set; }
    }
    class Box//box包装箱,包装箱有自己的属性Product属性箱子所包装的产品
    {
        public Product Product { get; set; }
    }

    class WrapFactor//把产品包装成产品交给用户
    {
        public Box WraapProduct(Func<Product> getProduct)
            //模板方法,接收一个委托类型的参数,委托类型选择Func委托,所分装的方法返回一个Program类的对象,是返回值类型
        {
            Box box = new Box();
            Product product = getProduct.Invoke();
            box.Product = product;
            return box;
        }
    }
   
    class ProductFactory
    {
        public Product MakePizza()//此方法生产披萨饼
        {
            Product product = new Product();
            product.Name = "Pizza";
            return product;
        }
        public Product MakeToCar()//此方法生产小汽车
        {
            Product product = new Product();
            product.Name = "To Car";
            return product;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值