C#委托简单使用

来源:互联网 发布:淘宝上的积分怎么用 编辑:程序博客网 时间:2024/05/21 10:59
//*****************************************/// <summary>/// General method.2017.../cyl/// </summary>///*****************************************using UnityEngine;using System.Collections;using System;using System.IO;public delegate int NumberChange (int n);public delegate void printString (string str);public class TestScritpDelegate : MonoBehaviour{    // Use this for initialization    void Start ()    {//      Method();//      Method2();//      method3 ();        Method4();    }    //************************************************************************************************    static int num = 10;    public static int AddNum (int p)    {        num += p;        Debug.Log ("add--" + num);        return num;    }    public static  int MultNum (int q)    {        num *= q;        Debug.Log ("mul---" + num);        return  num;    }    public static int getNum ()    {        return num;    }    //************************************************************************************************    //委托的使用1    void Method ()    {        //创建按委托实例        NumberChange nc1 = new NumberChange (AddNum);        NumberChange nc2 = new NumberChange (MultNum);        //使用委托对象调用方法        nc1 (25);        Debug.Log ("当前方法AddNum: " + getNum ());        nc2 (5);        Debug.Log ("当前方法MulNum: " + getNum ());    }    //************************************************************************************************    //委托的多播使用    void Method2 ()    {        Debug.Log ("委托的多播使用");        NumberChange nc;        NumberChange nc1 = new NumberChange (AddNum);        NumberChange nc2 = new NumberChange (MultNum);        //方法叠加        nc = nc1;        nc += nc2;        nc (5);        Debug.Log ("最后返回的结果: " + getNum ());    }    //************************************************************************************************    //委托的使用2    void method3 ()    {        printString ps1 = new printString (WriteScreen);        printString ps2 = new printString (WritetoFile);        sendString (ps1);        sendString (ps2);    }    public void WriteScreen (string str)    {        Debug.Log ("绑定的WriteScreen方法.打印了str--->: " + str);    }    //文件写入保存    public void WritetoFile (string str)    {        Debug.Log ("绑定了WritetoFile,并写入了文件中保存");        FileStream fs;        StreamWriter sw;        fs = new FileStream (Application.dataPath + "/MM.txt", FileMode.Append, FileAccess.Write);        sw = new StreamWriter (fs);        sw.WriteLine (str);        sw.Flush ();        sw.Close ();        sw.Dispose ();        fs.Close ();    }    public void sendString (printString ps)    {        ps ("hahahaha");    }    void Method4 ()    {        MrMing ming=new MrMing();    }}// 小明叫小张去买一张车票,然后再去买一张电影票************************************************************************************************public class MrZhang{    public static void BuyTicket ()    {        Debug.Log ("小张替小明买了一张票!");    }    public static void BuyMovieTicket ()    {        Debug.Log ("小张替小明去买了电影票");    }}public class MrMing{    public delegate void BuyTicketEventHandler ();    public MrMing(){        //这里是具体命令阐述着,叫小张去买票        BuyTicketEventHandler myDelegate = new BuyTicketEventHandler (MrZhang.BuyTicket);        //之后再追加买电影票        myDelegate += MrZhang.BuyMovieTicket;        //这时候委托被附上具体方法        myDelegate ();    }}
0 0