首页 > 编程语言 >c#学习-(委托的高级使用)

c#学习-(委托的高级使用)

时间:2024-06-03 20:31:25浏览次数:19  
标签:Product 委托 c# System 高级 Student using Action new

一、多播委托(multicast)&& 单播委托

        一个委托内部封装不止一个方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace six_multicast
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student() { Id=1,PenColor = ConsoleColor.Yellow};
            Student student2 = new Student() { Id = 2,PenColor = ConsoleColor.Red};
            Student student3 = new Student() { Id = 3, PenColor = ConsoleColor.Green };

            Action action1 = new Action(student.DoHomeWork);
            Action action2 = new Action(student2.DoHomeWork);
            Action action3 = new Action(student3.DoHomeWork);

            //单播
            action1.Invoke();
            action2.Invoke();
            action3.Invoke();

            //多播
            action1 += action2;
            action1 += action3;

            action1.Invoke();
        }
    }
    class Student
    {
        public int Id { get; set; }
        public ConsoleColor PenColor { get; set; }

        public void DoHomeWork()
        {
            for (int i = 0; i < 5; i++) 
            {
                Console.ForegroundColor = this.PenColor;
                Console.WriteLine("Student is {0} do homework {1} hour(s)", this.Id, i);
                Thread.Sleep(1000);
            }
        }
    }

}

二、同步调用

每一个允许的程序是一个进程(Process)

每个进程可以有多个线程(Thread)

同步调用是在一个线程里

串行==同步==单线程

同步调用三种形式

        1、直接同步调用

        2、单播委托的间接调用

        3、多播委托的间接调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace six_multicast
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student() { Id=1,PenColor = ConsoleColor.Yellow};
            Student student2 = new Student() { Id = 2,PenColor = ConsoleColor.Red};
            Student student3 = new Student() { Id = 3, PenColor = ConsoleColor.Green };

            Action action1 = new Action(student.DoHomeWork);
            Action action2 = new Action(student2.DoHomeWork);
            Action action3 = new Action(student3.DoHomeWork);
            //同步调用
            //在一根主线程上面执行(创建对象),当有方法执行的时候就去执行方法,然后继续执行主线程,最后主线程还有些事情要做
            //直接调用
            student.DoHomeWork();
            student2.DoHomeWork();
            student3.DoHomeWork();

            //单播(单波委托的间接调用)
            action1.Invoke();
            action2.Invoke();
            action3.Invoke();

            //多播(多播委托的间接调用)
               action1 += action2;
               action1 += action3;

               action1.Invoke();


            for (int i = 0; i < 10; i++)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Main thread {0}", i);
                Thread.Sleep(1000);
            }
        }
    }
    class Student
    {
        public int Id { get; set; }
        public ConsoleColor PenColor { get; set; }

        public void DoHomeWork()
        {
            for (int i = 0; i < 5; i++) 
            {
                Console.ForegroundColor = this.PenColor;
                Console.WriteLine("Student is {0} do homework {1} hour(s)", this.Id, i);
                Thread.Sleep(1000);
            }
        }
    }

}

三、异步调用

同时进行,多个线程,底层原理=多线程

并行=异步=多线程

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace six_multicast
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student() { Id=1,PenColor = ConsoleColor.Yellow};
            Student student2 = new Student() { Id = 2,PenColor = ConsoleColor.Red};
            Student student3 = new Student() { Id = 3, PenColor = ConsoleColor.Green };

            Action action1 = new Action(student.DoHomeWork);
            Action action2 = new Action(student2.DoHomeWork);
            Action action3 = new Action(student3.DoHomeWork);

            //异步调用(多个线程争抢一个资源,会起冲突)
            //显示异步掉用(老方式)
            Thread thread = new Thread(new ThreadStart(student.DoHomeWork));
            Thread thread2 = new Thread(new ThreadStart(student2.DoHomeWork));
            Thread thread3 = new Thread(new ThreadStart(student3.DoHomeWork));

            thread.Start();
            thread2.Start();
            thread3.Start();
            //显示异步调用(新方式)
            Task task = new Task(new Action(student.DoHomeWork));
            Task task1 = new Task(new Action(student2.DoHomeWork));
            Task task2 = new Task(new Action(student3.DoHomeWork));

            task.Start();
            task1.Start();
            task2.Start();
            //隐式异步调用
            action1.BeginInvoke(null, null);
            action2.BeginInvoke(null, null);
            action3.BeginInvoke(null, null);


            for (int i = 0; i < 10; i++)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Main thread {0}", i);
                Thread.Sleep(1000);
            }
        }
    }
    class Student
    {
        public int Id { get; set; }
        public ConsoleColor PenColor { get; set; }

        public void DoHomeWork()
        {
            for (int i = 0; i < 5; i++) 
            {
                Console.ForegroundColor = this.PenColor;
                Console.WriteLine("Student is {0} do homework {1} hour(s)", this.Id, i);
                Thread.Sleep(1000);
            }
        }
    }

}

总结:

        应该慎重的使用委托

        对于为什么会造成内存泄漏和程序性能下降原因

        委托会引用一个方法,这个方法如果是一个实例方法的话,那么这个方法必定会隶属于一个对象,你拿一个委托引用了这个方法,那么这个对象就必须存在于内存当中,即便是没有其他的引用变量引用这个对象,因为你一释放这个内存,委托就不能够间接去调用对象的方法,所以所委托有可能造成内存泄漏,随着泄漏的内存越来越多,程序就会崩溃

        使用异步会导致出现多个线程争抢资源的情况,会出现冲突,可以加上线程锁,另外,应该合适的使用接口interface取代一些对委托的使用

        下面两个分别是使用接口interface与使用委托delegate,最终的实现都是一样

        

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace six_InterfaceDelegate
{
    internal class Program
    {
        static void Main(string[] args)
        {
            WrapFactory wrapFactory = new WrapFactory();
            IProductFactory MakePizza = new MakePizza();
            IProductFactory MakeCar = new MakeCar();

            Box box = wrapFactory.ProductFactory(MakePizza);
            Box box2 = wrapFactory.ProductFactory(MakeCar);

            Console.WriteLine(box.Product.Name);
            Console.WriteLine(box2.Product.Name);
        }
    }

    interface IProductFactory
    {
        Product Make();
    }
    class MakePizza : IProductFactory
    {
        public Product Make()
        {
            Product product = new Product();
            product.Name = "Pizza";
            return product;
        }
    }

    class MakeCar : IProductFactory
    {
        public Product Make()
        {
            Product product = new Product();
            product.Name = "Car";
            return product;
        }
    }
    class Product
    {
        public string Name { get; set; }
    }

    class Box
    {
        public Product Product { get; set; }
    }

    class WrapFactory
    {
        public Box ProductFactory(IProductFactory productFactory)
        {
            Box box = new Box();
            Product product = productFactory.Make();
            box.Product = product;
            return box;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
    演示了如何使用委托和模板方法来包装产品
 */
namespace four_GreateDelegate2
{
    internal class Program
    {
        static void Main(string[] args)
        {
           
            ProductFactory productFactory = new ProductFactory();
            
            WrapFactory wrapFactory = new WrapFactory();
            
            Func<Product> func1 = new Func<Product>(productFactory.MakePizza);
            Func<Product> func2 = new Func<Product>(productFactory.MakeToCar);

            
            
            Box box1 = wrapFactory.WrapProduct(func1);
            Box box2 = wrapFactory.WrapProduct(func2);

            Console.WriteLine(box1.Product.Name);
            Console.WriteLine(box2.Product.Name);

        }
    }

    class Product//产品
    {
        public string Name { get; set; }
    }

    class Box//箱子
    {
        public Product Product { get; set; }
    }

    class WrapFactory
    {
       
        public Box WrapProduct(Func<Product> getProduct)
        {
            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 = "Toy Car";
            return product ;
        }
    }
}

标签:Product,委托,c#,System,高级,Student,using,Action,new
From: https://blog.csdn.net/weixin_66365687/article/details/139395878

相关文章

  • c++——vector
    c++——vectorvector的介绍vector的简介迭代器的作用vector的迭代器失效问题可能导致vector迭代器失效的操作vector的模拟实现完整代码vector.hTest.h代码测试结果vector的介绍vector的文档介绍vector的简介vector是表示可变大小数组的序列容器。vector采用的连......
  • [Cesium]设置点Entity的
    在Cesium中,修改点实体的样式可以通过使用billboard属性来设置自定义图标。为了使用自定义的AntDesign或VueIcon库中的图标,需要先将图标转换成可以在Cesium中使用的格式,例如Canvas或者DataURL。以下是一个示例,展示了如何使用AntDesign图标库中的图标并将其设置为Cesium中的......
  • 飞书企业自建项目接入ChatGPT搭建智能机器人并发布公网远程使用
    ......
  • WPF canvas mousewheel to zoom in or out
    <Windowx:Class="WpfApp133.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • ABC 310 E NAND repeatedly
    题意太懒了,直接给链接吧,题意挺好懂的。https://atcoder.jp/contests/abc310/tasks/abc310_e思路NAND运算,根据题意,我们可以总结出以下两点:当前结果如果遇到1,那么结果反转(0->1,1->0)当前结果如果遇到0,那么结果赋值为1我们手模一下这个样例1:00110(初始)01011(i==1)×0101(i==......
  • GCD构造排列
    题目给定一个长度为n的数组\(a\),试复原长度为n的排列\(p\)其中\(a_i=gcd(p_1,p_2,...,p_i)\),也就是说,\(a_i\)表示排列\(p\)中前\(i\)个数字的最大公约数。(由于数组\(a\)可能是错误的,故有可能无解,此时输出\(-1\)即可)https://ac.nowcoder.com/acm/problem/269091Input输......
  • java 线程池 Executors原生三大方法
    packagecom.chen.pool;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;publicclassTest1{publicstaticvoidmain(String[]args){//平时我们创建一些类使用工具类操作s//总数可以管理//线程池Executors原生三大方法......
  • OPEN_CV多线程线程池管理
    两种自旋锁的设置操作系统级别自旋锁的设置,例如在C++11及以后的版本中,自带线程管理库,可以定义为:defineCV_YIELD()std::this_thread::yield(),此时进入CV_YIELD(),线程释放CPU,线程被阻塞,等待被唤醒.CPU级别的自旋锁的设置,与使用的CPU架构有关,以X86为例,X86......
  • [C++] 小游戏 斗破苍穹 2.2.1至2.11.5所有版本(下) zty出品
    2.10.6#include<stdio.h>#include<iostream>#include<ctime>#include<bits/stdc++.h>#include<time.h>#include<windows.h>//SLEEP函数usingnamespacestd;intboss1=0,boss2=0;structPlayer{//玩家结构体,并初始化playercharname[21......
  • anaconda怎么运行python
    一、在AnacondaNavigator中运行首先点击菜单栏中的“开始”,在搜索栏中输入“Anaconda”,然后选择“AnacondaNavigator”。进入主界面,点击“Spyder”中的“Launch”即可。然后按F5键运行即可。二、在AnacondaPrompt中运行也可以在AnacondaPrompt中运行,点击菜单栏......