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");
}
}
运行结果:
注意事项:
继承自同一基接口的接口不要在类继承中使用以免被覆盖