zoukankan      html  css  js  c++  java
  • c#1所搭建的核心基础之委托

    本文将对c#1的委托进行详细探索

    委托(delegate)   注 delegate:vt.委派代表授权给[法律]债务转移

    委托作用:在恰当的时间执行一系列操作

    1.简单委托的构成

    • 声明委托类型
    • 必须有一个方法包含了要执行的代码
    • 必须创建一个委托实例
    • 必须调用委托实例

    依次讨论各个步骤:

    ①声明委托类型

    委托类型实际上只是参数类型的一个列表以及一个返回类型。它规定了类型的实例能表示的操作。

    例子:delegate void StringProcessor(string input)

    例子表明:要创建StringProcssor的一个实例需要一个返回值为void,有一个参数的函数。StringProcessor是一个类型。它有方法,可以创建他的实例。

    ②为委托实例的操作找到一个恰当的方法

    void printString(string x)

    ③创建委托实例

    StringProcessor proc1,proc2;

    proc1=new StringProcessor(StaticMethods.PrintString);//静态方法

    InstantMethods instance=new InstantMethods();//实例方法

    proc2=new StringProcessor(instance.PrintString);

    注:如果在同一个类中,直接将方法名作为参数

    当委托实例被调用时,就会为这个对象调用方法。单纯创建一个委托实例却不在某一时刻调用它是没有什么意义的。

    ④调用委托实例

    proc1.Invoke("hello");

    ⑤一个完整的例子

    using System

    delegate void StringProcessor(string input);//声明委托类型

    class Person

    {

    string name;

    public Person(string name){this.name=name}

    public void say(string message) //声明兼容的实例方法

    {

    Console.WriteLine("{0} says:{1}",name,message);

    }

    }

    class Background

    {

    public static void Note(string note)

    {

    Console.WriteLine("({0})",note);

    }

    }

    class SimpleDelegateUse

    {

    static void Main()

    {

    Person jon=new Person("Jon");

    Person tom =new Persono("tom");

    StringProcessor jonsVoice,tomVoice,background;

    jonsVoice=new StringProcessor(jon.Say);

    tomVoice=new StringProcessor(tom.say);

    baceground =new StringProcessor(Background.Note);

    jonsVoice("Hello,son.");

    tomsVoices.Invoke("Hello,Daddy!");

    background("An airplane flies past");

    }

    }

    合并和删除委托

    委托实例实际有一个操作列表与之关联。这称为委托实例的调用列表。

    System.Delegate类型的静态方法Combine和Remove负责创建新的委托实例。其中Combine负责将两个委托实例的调用列表连接到一起,而Remove负责从一个委托实例中删除另一个调用列表。很少在c#代码中看到对Delegate.Combine的显式调用,一般都是使用+和+=完成的

    具体例子

    using System;

    // Define a custom delegate that has a string parameter and returns void.
    delegate void CustomDel(string s);

    class TestClass
    {
        // Define two methods that have the same signature as CustomDel.
        static void Hello(string s)
        {
            System.Console.WriteLine("  Hello, {0}!", s);
        }

        static void Goodbye(string s)
        {
            System.Console.WriteLine("  Goodbye, {0}!", s);
        }

        static void Main()
        {
            // Declare instances of the custom delegate.
            CustomDel hiDel, byeDel, multiDel, multiMinusHiDel;

            // In this example, you can omit the custom delegate if you
            // want to and use Action<string> instead.
            //Action<string> hiDel, byeDel, multiDel, multiMinusHiDel;

            // Create the delegate object hiDel that references the
            // method Hello.
            hiDel = Hello;

            // Create the delegate object byeDel that references the
            // method Goodbye.
            byeDel = Goodbye;

            // The two delegates, hiDel and byeDel, are combined to
            // form multiDel.
            multiDel = hiDel + byeDel;

            // Remove hiDel from the multicast delegate, leaving byeDel,
            // which calls only the method Goodbye.
            multiMinusHiDel = multiDel - hiDel;

            Console.WriteLine("Invoking delegate hiDel:");
            hiDel("A");
            Console.WriteLine("Invoking delegate byeDel:");
            byeDel("B");
            Console.WriteLine("Invoking delegate multiDel:");
            multiDel("C");
            Console.WriteLine("Invoking delegate multiMinusHiDel:");
            multiMinusHiDel("D");
        }
    }

    整理自:《深入理解c#》第二版(c# in Depth)

  • 相关阅读:
    Spark 集群 任务提交模式
    Spark RDD 宽窄依赖
    Spark 资源调度包 stage 类解析
    堆内内存与堆外内存
    Spark笔记(一)
    scala 语言特性
    Centos7.4 Storm2.0.0 + Zookeeper3.5.5 高可用集群搭建
    fs模块他的作用是(文件夹)
    事件循环机制
    简单的下路由(我们可以在控制器中看到路由参数)
  • 原文地址:https://www.cnblogs.com/wjcnet/p/3393499.html
Copyright © 2011-2022 走看看