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

运行结果:

注意事项:

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

相关推荐
可喜~可乐30 分钟前
C# WPF开发
microsoft·c#·wpf
666和7775 小时前
C#的单元测试
开发语言·单元测试·c#
小码编匠6 小时前
WPF 星空效果:创建逼真的宇宙背景
后端·c#·.net
向宇it8 小时前
【从零开始入门unity游戏开发之——unity篇02】unity6基础入门——软件下载安装、Unity Hub配置、安装unity编辑器、许可证管理
开发语言·unity·c#·编辑器·游戏引擎
yngsqq9 小时前
一键打断线(根据相交点打断)——CAD c# 二次开发
windows·microsoft·c#
TENET信条9 小时前
day53 第十一章:图论part04
开发语言·c#·图论
anlog11 小时前
C#在自定义事件里传递数据
开发语言·c#·自定义事件
向宇it12 小时前
【从零开始入门unity游戏开发之——unity篇01】unity6基础入门开篇——游戏引擎是什么、主流的游戏引擎、为什么选择Unity
开发语言·unity·c#·游戏引擎
仰望大佬00713 小时前
Avalonia实例实战五:Carousel自动轮播图
数据库·microsoft·c#
糖朝13 小时前
c#读取json
c#·json