代理:指向方法的指针。
实现了调用其他类的时候不用写引用
例子如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class People
{
public void RepayMoney()
{
Debug.Log("OK,soon");
}
}
public class Bank
{
public delegate void Worker();
public Worker worker;
public void Repay()
{
worker();
}
}
public class TestDelegate : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
People people = new People();
Bank bank = new Bank();
bank.worker += people.RepayMoney;
bank.Repay();
}
// Update is called once per frame
void Update()
{
}
}
|