[英]Passing type parameter to interface of base class
StateBase implements iState StateBase实现iState
WolfState ineherits from StateBase WolfState从StateBase继承
WolfController inherits from ControllerBase WolfController从ControllerBase继承
I want to be able to do a 我希望能够做到
WolfController and SheepController would both inherit from StateController. WolfController和SheepController都将从StateController继承。
How should i declare WolfState? 我应该如何声明WolfState?
The way i try to do it doesnt work. 我尝试做的方式不起作用。
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>
{
}
It looks like you intend State<T>
to implement IState<T>
. 看起来您打算让
State<T>
实现IState<T>
。 You currently have a constraint on the type of T
. 当前,您对
T
的类型有约束。 Change the definition of State<T>
to: 将
State<T>
的定义更改为:
public abstract class State<T> : IState<T> where T : StateController
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.