C#高级---代理模式

【千锋合集】史上最全Unity3D全套教程|匠心之作_哔哩哔哩_bilibili

 

 

 

初级

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

public class CsharpProxy : MonoBehaviour {

    // Use this for initialization
    void Start () {
        PayFine self = new NormalDriver("老王","1111232321311","123123123");
        FriendDriver friendProxy = new FriendDriver("小王", "111166666311", "123166623");
        //自己交罚款
        self.Pay(200, 3);
        //建立代理关系
        friendProxy.realDriver = self;
        friendProxy.Pay(200, 3);

    }    
}

public abstract class PayFine {
    public string name;
    /// <summary>
    /// 违章者的身份证
    /// </summary>
    public string violatorID;
    /// <summary>
    /// 违章者驾驶证
    /// </summary>
    public string violatorDriverID;
    

    protected PayFine(string name, string violatorID, string violatorDriverID)
    {
        this.name = name;
        this.violatorID = violatorID;
        this.violatorDriverID = violatorDriverID;
    }

    /// <summary>
    /// 支付罚款和扣除分数
    /// </summary>
    /// <param name="money"></param>
    /// <param name="score"></param>
    public abstract void Pay(float money,float score);
}


public class NormalDriver : PayFine
{
    public NormalDriver(string name, string violatorID, string violatorDriverID) : base(name, violatorID, violatorDriverID)
    {
    }

    public override void Pay(float money, float score)
    {
        Debug.Log(string.Format("我是{0},身份证号{1},驾驶证号{2},今交罚款{3},扣除分数{4}",
            name,violatorID,violatorDriverID,money,score));
    }
}

public class FriendDriver : PayFine
{
    /// <summary>
    /// 正真的驾驶员,如果没有,设置为null
    /// </summary>
    public PayFine realDriver;

    public FriendDriver(string name, string violatorID, string violatorDriverID) : base(name, violatorID, violatorDriverID)
    {
    }

    public override void Pay(float money, float score)
    {
        if (realDriver!=null)
        {
            Debug.Log("代理人"+name);
            realDriver.Pay(money, score);
        }
    }
}

高级

using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

public class UnityProxy : MonoBehaviour {

    // Use this for initialization
    void Start () {
        gameObject.AddComponent<DelayProxy>();
        //延时执行方法
        //DelayProxy.instance.DelayCall(ShowTime,3);
        
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) {
            //获取方法
            MethodInfo methodInfo = this.GetType().GetMethod("ShowCurrentTime");
            //通过反射延时执行方法
            DelayProxy.instance.DelayCallByReflection(methodInfo, this, new object[] { Time.time }, 5);
        }
    }
    public void ShowTime() {
        Debug.Log("Time:"+Time.time);        
     }
    public void ShowCurrentTime(float crtTime) {
        Debug.Log("启动延时时间"+crtTime);
        Debug.Log("执行时间:"+Time.time);
    }
}

public class DelayProxy :MonoBehaviour{
    public static DelayProxy instance;
    private void Awake()
    {
        instance = this;
        
    }
    public void DelayCallByReflection(MethodInfo methodInfo,object obj,object[] parameters, float delayTime=3f ) {
        StartCoroutine(IEDelayCallByReflection(methodInfo, obj, parameters, delayTime));
    }
    IEnumerator IEDelayCallByReflection(MethodInfo methodInfo, object obj, object[] parameters, float delayTime)
    {
        //等待几秒
        yield return new WaitForSeconds(delayTime);
        methodInfo.Invoke(obj,parameters);
    }
    public void DelayCall(System.Action action,float delayTime=3f) {
        StartCoroutine(IEDelayCall(action,delayTime));
    }
    IEnumerator IEDelayCall(System.Action action, float delayTime) {
        //等待几秒
        yield return new WaitForSeconds(delayTime);
        action();
    }
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值