Delegate,Action,Func的使用

我写了一段简单的代码,自己看代码吧,对比的看很容易明白,定义什么的自己百度吧

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

public class TestClass : MonoBehaviour
{
    public delegate void DelegateT(int age,bool boolean);
    
        // Start is called before the first frame update
    void Start()
    {
        Action<int,bool> a = test;
        DelegateT delegateT = test;
        ShowInformation("程",(s,b)=>a(s,b));
        
        ShowInformation("M",(s,b)=>delegateT(s,b));
        
        ShowInformationDelegateT("DelegateT",(s,b)=>delegateT(s,b));
        // ShowInformation("梅", (s, b) =>a(s=18,b=false));
        // ShowInformation("梅", (s, b) => Debug.Log($"年龄:{19},毕业{false}"));
        Func<string, bool,string> t01 = test01;
        ShowInformation01("梅",(sex,b)=>t01(sex,b));
    }

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

    void ShowInformation(string name,Action<int , bool> action)
    {
        Debug.Log(name);
        action(19,false);
    }
    void ShowInformationDelegateT(string name,DelegateT action)
    {
        Debug.Log(name);
        action(19,false);
    }

    void ShowInformation01(string name,Func<string,bool,string> func)
    {
        Debug.Log($"姓名:{name}");
        Debug.Log(func("男",true));
    }
    
    void test(int age,bool boolean)
    {
        Debug.Log($"年龄:{age},毕业{boolean}");
    }

   string test01(string sex,bool boolean)
   {
     return ($"性别:{sex},成年:{boolean}");
   }

}

猜你喜欢

转载自blog.csdn.net/qq_35385242/article/details/127572946