C#接口的一些坑

cs 复制代码
using System;
using LXF_Framework;
using UnityEngine;

public class Test : Card_ATTACK_test
{
    
}



public interface IInit<T, TParam> where T : IInit<T, TParam>
{
    void Set(Action<TParam> action)
    {
        GetEventRunner().Clear();
        GetEventRunner().Register(action);
    }

    void ReSet()
    {
        Debug.Log("Reset");
        GetEventRunner().Clear();
        GetEventRunner().Register(GetDefaultAction());
    }



    EventRunner<TParam> GetEventRunner();
    Action<TParam> GetDefaultAction();
}

public interface IAttack_test : IInit<IAttack_test, (int, BaseCard)>
{
    EventRunner<(int, BaseCard)> OnAttackEvent { get; set; }
    void Attack(int damage, BaseCard card);

    Action<(int, BaseCard)> IInit<IAttack_test, (int, BaseCard)>.GetDefaultAction(){
        Debug.Log("IAttack_test: GetDefaultAction");
        return tuple => Attack(tuple.Item1, tuple.Item2);
     }
        
    EventRunner<(int, BaseCard)> IInit<IAttack_test, (int, BaseCard)>.GetEventRunner(){
        Debug.Log("IAttack_test: GetEventRunner");
        return OnAttackEvent;
    }
}


public abstract class Card_ATTACK_test : CARD,IAttack_test
{
    
    protected override void Start()
    {
        base.Start();
        ((IAttack_test)this).ReSet();
    }
}

public interface IHurt_test : IInitialization<IHurt, int>
{
    EventRunner<int> OnHurtEvent { get; set; }
    void GetHurt(int damage);

    Action<int> IInitialization<IHurt, int>.GetDefaultAction() {
        Debug.Log("IHurt_test: GetDefaultAction");
        return damage => GetHurt(damage);
    }
    EventRunner<int> IInitialization<IHurt, int>.GetEventRunner() {
        Debug.Log("IHurt_test: GetEventRunner");
        return OnHurtEvent;
    }
}


public abstract class CARD :MonoBehaviour, IHurt_test
{
    public EventRunner<int> OnHurtEvent { get; set ; }=new();

    public void GetHurt(int damage)
    {
        
    }

    protected virtual void Start() {
        ((IHurt_test)this).ReSet();
    }

    public EventRunner<(int, BaseCard)> OnAttackEvent { get; set; }=new EventRunner<(int, BaseCard)> ();

  
    public void Attack(int damage, BaseCard card)
    {
        Debug.Log("Attack");
    }
}

运行结果:

说明:当两个接口同时继承自一个基础接口IBASE时,当一个类A实现其中某一个接口IA时,再用其他类B来继承这个类时,类B所实现的所有基础接口IBASE的接口都被失效(被IA覆盖)

当两个接口处于同一级别时,才会正确执行

cs 复制代码
public abstract class CARD :MonoBehaviour, IHurt_test,IAttack_test
cs 复制代码
using System;
using LXF_Framework;
using UnityEngine;

public class Test : Card_ATTACK_test
{
    
}



public interface IInit<T, TParam> where T : IInit<T, TParam>
{
    void Set(Action<TParam> action)
    {
        GetEventRunner().Clear();
        GetEventRunner().Register(action);
    }

    void ReSet()
    {
        Debug.Log("Reset");
        GetEventRunner().Clear();
        GetEventRunner().Register(GetDefaultAction());
    }



    EventRunner<TParam> GetEventRunner();
    Action<TParam> GetDefaultAction();
}

public interface IAttack_test : IInit<IAttack_test, (int, BaseCard)>
{
    EventRunner<(int, BaseCard)> OnAttackEvent { get; set; }
    void Attack(int damage, BaseCard card);

    Action<(int, BaseCard)> IInit<IAttack_test, (int, BaseCard)>.GetDefaultAction(){
        Debug.Log("IAttack_test: GetDefaultAction");
        return tuple => Attack(tuple.Item1, tuple.Item2);
     }
        
    EventRunner<(int, BaseCard)> IInit<IAttack_test, (int, BaseCard)>.GetEventRunner(){
        Debug.Log("IAttack_test: GetEventRunner");
        return OnAttackEvent;
    }
}


public abstract class Card_ATTACK_test : CARD
{
    
    protected override void Start()
    {
        base.Start();
        ((IAttack_test)this).ReSet();
    }
}

public interface IHurt_test : IInitialization<IHurt, int>
{
    EventRunner<int> OnHurtEvent { get; set; }
    void GetHurt(int damage);

    Action<int> IInitialization<IHurt, int>.GetDefaultAction() {
        Debug.Log("IHurt_test: GetDefaultAction");
        return damage => GetHurt(damage);
    }
    EventRunner<int> IInitialization<IHurt, int>.GetEventRunner() {
        Debug.Log("IHurt_test: GetEventRunner");
        return OnHurtEvent;
    }
}


public abstract class CARD :MonoBehaviour, IHurt_test,IAttack_test
{
    public EventRunner<int> OnHurtEvent { get; set ; }=new();

    public void GetHurt(int damage)
    {
        
    }

    protected virtual void Start() {
        ((IHurt_test)this).ReSet();
    }

    public EventRunner<(int, BaseCard)> OnAttackEvent { get; set; }=new EventRunner<(int, BaseCard)> ();

  
    public void Attack(int damage, BaseCard card)
    {
        Debug.Log("Attack");
    }
}

运行结果:

注意事项:

继承自同一基接口的接口不要在类继承中使用以免被覆盖

相关推荐
“抚琴”的人17 小时前
【机械视觉】C#+VisionPro联合编程———【六、visionPro连接工业相机设备】
c#·工业相机·visionpro·机械视觉
FAREWELL0007519 小时前
C#核心学习(七)面向对象--封装(6)C#中的拓展方法与运算符重载: 让代码更“聪明”的魔法
学习·c#·面向对象·运算符重载·oop·拓展方法
CodeCraft Studio20 小时前
Excel处理控件Spire.XLS系列教程:C# 合并、或取消合并 Excel 单元格
前端·c#·excel
勘察加熊人21 小时前
forms实现连连看
c#
hvinsion21 小时前
PPT助手:一款集计时、远程控制与多屏切换于一身的PPT辅助工具
c#·powerpoint·ppt·ppt助手·ppt翻页
weixin_307779131 天前
使用C#实现从Hive的CREATE TABLE语句中提取分区字段名和数据类型
开发语言·数据仓库·hive·c#
时光追逐者1 天前
在 Blazor 中使用 Chart.js 快速创建数据可视化图表
开发语言·javascript·信息可视化·c#·.net·blazor
与火星的孩子对话1 天前
Unity3D开发AI桌面精灵/宠物系列 【三】 语音识别 ASR 技术、语音转文本多平台 - 支持科大讯飞、百度等 C# 开发
人工智能·unity·c#·游戏引擎·语音识别·宠物
response_L1 天前
国产系统统信uos和麒麟v10在线打开word给表格赋值
java·c#·word·信创·在线编辑
MasterNeverDown1 天前
Swagger2Md:让WebAPI文档生成变得轻松高效
c#