将类型参数传递给基类的接口



StateBase 实现 iState

WolfState inherits from StateBase

WolfController 继承自 ControllerBase

我希望能够做一个

  • 使用 WolfController 继承自 State 的 WolfState
  • 绵羊状态使用SheepController并从State继承

WolfController 和 SheepController 都将继承自 StateController。

我应该如何声明狼州?

我尝试这样做的方式不起作用。

   public interface IState <T> where T : StateController 
{
}
public abstract class State<T> where T : StateController,  IState<T> 
{
}
// THIS IS HOW I WOULD LIKE TO DO IT BUT ITS NOT ACCEPTED
public class WolfState : State<WolfController>
{
}
public class SheepState : State<SheepController>
{
}

看起来你打算State<T>实现IState<T> .您当前对T类型有约束。将State<T>的定义更改为:

public abstract class State<T> : IState<T> where T : StateController
{
}

最新更新