委托

来源:互联网 发布:淘宝标题自动优化软件 编辑:程序博客网 时间:2024/06/04 19:07

Action是无返回值的泛型委托。

   Action 表示无参,无返回值的委托

   Action<int,string> 表示有传入参数int,string无返回值的委托

   Action<int,string,bool> 表示有传入参数int,string,bool无返回值的委托

       Action<int,int,int,int> 表示有传入4个int型参数,无返回值的委托

   Action至少0个参数,至多16个参数,无返回值。

using UnityEngine;

using System.Collections;
using System;

public class TestAction : MonoBehaviour {

    // Use this for initialization
    void Start () {
        Test<string> (Action, "字符串");    //T-string >> 字符串
        Test<int> (Action, 1000);        //T-int >> 1000
        Test<string> (p => {Debug.Log("lambda表达式定义委托 >> " + p);}, "我用的是表达式");//lambda表达式定义委托 >> 我用的是表达式
    }

    void Test<T>(Action<T> action, T p)
    {
        action (p);
    }

    void Action(string s)
    {
        Debug.Log ("T-string >> " + s);
    }

    void Action(int s)
    {
        Debug.Log ("T-int >> " + s);
    }

}



delegate

        delegate我们常用到的一种声明

    Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型。

using UnityEngine;
using System.Collections;

public delegate int MethodDelegate(int x, int y);

public class TestDelegate : MonoBehaviour {

    private MethodDelegate method;

    // Use this for initialization
    void Start () {
        method = new MethodDelegate (Add);
        Debug.Log (method(10, 20));//30
    }

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

}


相关文章:http://www.cnblogs.com/akwwl/p/3232679.html


0 0
原创粉丝点击