C#委托简单使用

//*****************************************
/// <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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值