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");
    }
}

运行结果:

注意事项:

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

相关推荐
鲤籽鲲31 分钟前
C# MethodTimer.Fody 使用详解
开发语言·c#·mfc
工业3D_大熊1 小时前
3D可视化引擎HOOPS Luminate场景图详解:形状的创建、销毁与管理
java·c++·3d·docker·c#·制造·数据可视化
yngsqq1 小时前
c#使用高版本8.0步骤
java·前端·c#
hccee4 小时前
C# IO文件操作
开发语言·c#
广煜永不挂科6 小时前
Devexpress.Dashboard的调用二义性
c#·express
初九之潜龙勿用8 小时前
C#校验画布签名图片是否为空白
开发语言·ui·c#·.net
吾与谁归in10 小时前
【C#设计模式(13)——代理模式(Proxy Pattern)】
设计模式·c#·代理模式
吾与谁归in10 小时前
【C#设计模式(14)——责任链模式( Chain-of-responsibility Pattern)】
设计模式·c#·责任链模式
神仙别闹11 小时前
基于C#和Sql Server 2008实现的(WinForm)订单生成系统
开发语言·c#
向宇it20 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎