C#-delegate

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestDelegate : MonoBehaviour {
    //原文地址:http://www.cnblogs.com/akwwl/p/3232679.html
    //详细参考:http://www.fengfly.com/plus/view-209140-1.html
    //http://www.cnblogs.com/foolishfox/archive/2010/09/16/1827964.html
    /*
      4.委托的特点
      委托类似于 C++ 函数指针,但它们是类型安全的。
      委托允许将方法作为参数进行传递。
      委托可用于定义回调方法。
      委托可以链接在一起;例如,可以对一个事件调用多个方法。
      方法不必与委托签名完全匹配。

      5.总结:
      Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型
      Func可以接受0个至16个传入参数,必须具有返回值
      Action可以接受0个至16个传入参数,无返回值
      Predicate只能接受一个传入参数,返回值为bool类型
    */

    //声明一个delegate
    public delegate int CalSum(int x, int y);

    //定义一个delegate
    private CalSum calSum;

    /// <summary>
    /// predicate 是返回bool型的泛型委托
    /// predicate<int> 表示传入参数为int 返回bool的委托
    /// Predicate有且只有一个参数,返回值固定为bool
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="obj"></param>
    /// <returns></returns>
    public delegate bool MyPredicate<T>(T obj);

	// Use this for initialization
	void Start () {
        Test1();
        Test2();
        Test3();
        Test4();
	}

    #region TestDelegate
    public void Test1()
    {
        calSum = new CalSum(Add);
        Debug.Log(calSum(20, 50));
    }

    private int Add(int x, int y)
    {
        return x + y;
    }
    #endregion

    #region TestAction
    public void Test2()
    {
        /*
         * 可以使用 Action<T1, T2, T3, T4> 委托以参数形式传递方法,而不用显式声明自定义的委托。
         * 封装的方法必须与此委托定义的方法签名相对应。 
         * 也就是说,封装的方法必须具有四个均通过值传递给它的参数,并且不能返回值。 
         * (在 C# 中,该方法必须返回 void)通常,这种方法用于执行某个操作。
        */
        TestAction<string>(GetMyString, "hahaha");
        TestAction<int>(GetMyInt, 5);

        var action = new Action<string>(GetMyString);
        action("hello world");

        var action_1 = new Action<string, int>(GetMyStrAndInt);
        action_1("I am ", 24);
    }

    private void GetMyString(string str)
    {
        Debug.Log("str = " + str);
    }
    private void GetMyInt(int i)
    {
        Debug.Log("i = " + i);
    }

    private void GetMyStrAndInt(string str, int i)
    {
        Debug.Log(str + i);
    }

    //Action是无返回值的泛型委托。
    public void TestAction<T>(Action<T> action, T p)
    {
        action(p);
    }

    #endregion

    #region TestFunc
    public void Test3()
    {
        int result = TestFunc<int, int>(Func, 1, 2);
        Debug.Log("result = " + result);

        var func = new Func<string, string, int>(Func_1);
        int result_1 = func("A", "a");
        Debug.Log("result_1 = " + result_1);
    }

    private int Func(int x, int y)
    {
        return x + y;
    }

    private int Func_1(string s1, string s2)
    {
        return s1.CompareTo(s2);
    }

    //Func是有返回值的泛型委托
    public int TestFunc<T, T>(Func<T, T, int> func, T t1, T t2)
    {
        return func(t1, t2);
    }
    #endregion

    #region TestPredicate
    //泛型委托:表示定义一组条件并确定指定对象是否符合这些条件的方法。
    //此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素。

    //使用带有 Array.Find 方法的 Predicate 委托搜索数组。如果 i 是偶数,此委托表示的方法 match 将返回 true
    //Find 方法为数组的每个元素调用此委托,在符合测试条件的第一个点处停止。
    public void Test4()
    {
        int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7 };
        int result = Array.Find(array, match);
        Debug.Log("result = " + result);
    }

    private bool match(int i)
    {
        return i % 2 == 0;
    }

    #endregion


    public void ClearDelegate()
    {
        if (this.calSum != null)
        {
            this.calSum -= this.calSum;
        }
    }

    public void ClearAllDelegate()
    {
        Delegate[] delegates = this.calSum.GetInvocationList();
        for (int i = 0; i < delegates.Length; i++)
        {
            this.calSum -= delegates[i] as CalSum;
        }

        //while (this.calSum != null)
        //{
        //    this.calSum -= this.calSum;
        //}
    }

	// Update is called once per frame
	void Update () {
		
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值