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

运行结果:

注意事项:

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

相关推荐
小码编匠6 小时前
一款 C# 编写的神经网络计算图框架
后端·神经网络·c#
Envyᥫᩣ9 小时前
C#语言:从入门到精通
开发语言·c#
IT技术分享社区15 小时前
C#实战:使用腾讯云识别服务轻松提取火车票信息
开发语言·c#·云计算·腾讯云·共识算法
△曉風殘月〆1 天前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
逐·風1 天前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#
m0_656974741 天前
C#中的集合类及其使用
开发语言·c#
九鼎科技-Leo1 天前
了解 .NET 运行时与 .NET 框架:基础概念与相互关系
windows·c#·.net
九鼎科技-Leo1 天前
什么是 ASP.NET Core?与 ASP.NET MVC 有什么区别?
windows·后端·c#·asp.net·mvc·.net
.net开发1 天前
WPF怎么通过RestSharp向后端发请求
前端·c#·.net·wpf