Untiy3d-c#搞定函数之delegate,event, lambda,Func,Action

delegate:标示了函数回调的规范,能够拥有一个签名(signature),并且它只能持有与它的签名相匹配的方法的引用。它所实现的功能与C/C++中的函数指针十分相似。

using System;

namespace TestCSharp
{
    class Animation
    {
        //无返回无参数的delegate(委托、代理)
        public  delegate void PlayEndHandler();
        //定义event(事件),类型为上面定义的PlayEndHandler委托
        public event PlayEndHandler PlayEndCallBack;

        //有返回有参数
        public delegate string ActionHandler(int type,string message);
        public event ActionHandler ActionCallBack;

        public Animation()
        {
            //可以是成员方法
            PlayEndHandler handler = Move;
            handler();

            this.PlayEndCallBack = Move;
            this.PlayEndCallBack();

            //也可是静态方法
            PlayEndHandler runHandler = Run;
            runHandler();

            ActionHandler aHandler = Jump;
            aHandler(190,"You Jump!");

            //设置的方法必须和定义一致
            this.ActionCallBack = Jump;
            Console.WriteLine(this.ActionCallBack(10001,"Jumping"));
        }

        private void Move()
        {
            Console.WriteLine("Move");
        }

        private static void Run()
        {
            Console.WriteLine("Run");
        }

        private string Jump(int type,string message)
        {
            string content = String.Format("type:{0},message:{1}",type,message);
            Console.WriteLine(content);
            return content;
        }


    }
}

event:加入event关键字,编译器会自动针对事件生成一个私有的字段(与此事件相关的委托),以及两个访问器方法,即add访问器方法以及remove访问器方法,用于对事件的Subscribe注册及Unsubscribe注销(对事件使用+=和使用-=操作时就是调用的这两个方法)。实际上声明一个委托类型的字段也可以实现这些功能。 之所以采用event而不直接采用委托,还是为了封装。可以设想一下,如果直接采用公共的委托字段,类型外部就可以对此字段进行直接的操作了,比如将其直接赋值为null。 而使用event关键字就可以保证对事件的操作仅限于add访问器方法以及remove访问器方法(即只能使用+=及-=)

using System;

namespace TestCSharp
{
    class Animation
    {
        //无返回无参数的delegate(委托)
        public delegate void PlayEndHandler();
        //定义event(事件),类型为上面定义的PlayEndHandler委托
        public event PlayEndHandler PlayEndCallBack;



        public Animation()
        {

            this.PlayEndCallBack += Move;
            this.PlayEndCallBack += Run;

            this.PlayEndCallBack();

            this.PlayEndCallBack -= Run;
            this.PlayEndCallBack -= Move;
            if (this.PlayEndCallBack != null)
            {
                this.PlayEndCallBack();
            }

            PlayEndHandler d1 = Move;
            PlayEndHandler d2 = Run;
            this.PlayEndCallBack = d1 + d2;
            this.PlayEndCallBack -= Run;
            this.PlayEndCallBack();
            //Move,Run,Move
        }

        private void Move()
        {
            Console.WriteLine("Move");
        }

        private static void Run()
        {
            Console.WriteLine("Run");
        }

    }
}

lambda

using System;

namespace TestCSharp
{
    class Animation
    {
        //无返回无参数的delegate(委托)
        public delegate void PlayEndHandler();
        //定义event(事件),类型为上面定义的PlayEndHandler委托
        public event PlayEndHandler PlayEndCallBack;

        public delegate void RunEndHandler(string message);
        public event RunEndHandler RunCallBack;

        public Animation()
        {
            //委托回调
            this.PlayEndCallBack += Move;
            //匿名委托
            this.PlayEndCallBack += delegate { Console.WriteLine("Jump"); };
            //匿名lambda
            this.PlayEndCallBack += () => Console.WriteLine("Fly");

            this.PlayEndCallBack();

            this.RunCallBack += delegate(string name) { Console.WriteLine("Jump"); };
            this.RunCallBack += (string name) => Console.WriteLine("Fly");
            this.RunCallBack("cab");
        }

        private void Move()
        {
            Console.WriteLine("Move");
        }


        private void Run(string message)
        {
            Console.WriteLine("Run");
        }
    }
}



Func:有输入参数,还有返回值的泛型委托

using System;

namespace TestCSharp
{
    class Animation
    {
        //无参数,返回值为string
        public Func callback;
        //参数为bool,int,返回值为string
        public Func playCallback1;
        //有多种方法重载
        public Func playCallback2;

        public Animation()
        {
            this.callback = Move;
            Console.WriteLine(this.callback());

            this.playCallback1 = Run;
            Console.WriteLine(this.playCallback1(false,10001));

            this.playCallback2 = Run;
            Console.WriteLine(this.playCallback2(10002));
        }

        private string Move()
        {
            return "Move";
        }

        private string Run(bool isRun,int type)
        {
            return "Run1";
        }

        private string Run( int type)
        {
            return "Run2";
        }
    }
}



Action:只有输入参数,无返回值的泛型委托

using System;

namespace TestCSharp
{
    class Animation
    {
        //无参数,返回值为string
        public Action callback;
        //参数为bool,int,返回值为string
        public Action playCallback1;
        //有多种方法重载
        public Action playCallback2;

        public Animation()
        {
            this.callback = Move;
            this.callback("Hi");

            this.playCallback1 = Run;
            this.playCallback1(false, 10001);

            this.playCallback2 = Jump;
            this.playCallback2();
        }

        private void Move(string message)
        {
            Console.WriteLine("Move");
        }

        private void Run(bool isRun,int type)
        {
            Console.WriteLine("Run");
        }

        private void Jump()
        {
            Console.WriteLine("Jump");
        }
    }
}

predicate:返回bool型的泛型委托, predicate 表示传入参数为int ,返回bool的委托, Predicate有且只有一个参数,返回值固定为bool

using System;

namespace TestCSharp
{
    class Animation
    {

        public Predicate Callback;

        public Animation()
        {
            this.Callback = Move;
            Console.WriteLine(this.Callback(1));
        }

        private bool Move(int type)
        {
            Console.WriteLine("Move");
            return true;
        }

    }
}


你可能感兴趣的:(c#,unity3d,Unity3d语言基础,c#,lambda,delegate,action,callback)