【发布时间】:2017-03-29 13:17:29
【问题描述】:
StateBase 实现 iState
WolfState 从 StateBase 继承
WolfController 继承自 ControllerBase
我希望能够做一个
- WolfState 使用继承自 State 的 WolfController
- 绵羊状态 使用 SheepController 并继承自 State
WolfController 和 SheepController 都将继承自 StateController。
我应该如何声明 WolfState?
我尝试这样做的方式不起作用。
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>
{
}
【问题讨论】:
-
public class WolfState : State<WolfController>? -
@Lee 对不起!这就是我要写的。我已经更新了我的问题。所以这是行不通的。
-
WolfController是否扩展StateController?您收到的错误是什么? -
是的,没错。 WolfController 和 SheepController 都扩展了 StateController。 "没有从 'WolfController' 到 'IState
的隐式引用转换"
标签: c# generics inheritance polymorphism