委托

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值