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)

  • 相关阅读:
    韦德螺旋: 这真是一个螺旋吗?
    山上你能看到什么动物?
    你能够30秒内一字不差的念完它吗? 注意, 是读“颜色”, 不是让你识字.
    路透斯沃德的不可能的三角形
    换个角度, 青蛙也许就是白马王子
    这是一张很有趣的图片, 通常女性会先看到月亮, 男性会先看到人脸. 如果相反, 表示你体内的异性荷尔蒙偏高哦!
    亲吻的情侣幻觉: 这幅虚幻的亲吻由美国艺术家杰里•唐恩创作.
    PostgreSQL的 initdb 源代码分析之七
    PostgreSQL的initdb 源代码分析之六
    PostgreSQL的initdb 源代码分析之五
  • 原文地址:https://www.cnblogs.com/wjcnet/p/3393499.html
Copyright © 2011-2022 走看看