在WolfStateController.Start()中我试图将State TrueState设置为继承自State的WolfIdleState。
但WolfStateController扩展了StateController。仍然不被接受。看看Start方法我写了错误信息。这应该是可能的吗?要么我缺少关于多态性的东西,要么我的语法错了。
// STATECONTROLLER
public class StateController : MonoBehaviour
{
public State<StateController> CurrentState;
}
public class WolfStateController : StateController
{
void Start()
{
var idleState = new WolfIdleState();
var keyPressDecision = new KeyPressedDecision(KeyCode.Space);
// FOLLOWING GIVES ERROR:
// Cannot implicity convert WolfIdleState to State<StateController>
keyPressDecision.TrueState = idleState;
}
}
// STATE
public abstract class State<SC> where SC : StateController
{
}
public abstract class WolfState : State<WolfStateController>
{
}
public class WolfIdleState : WolfState
{
}
// DECISIONS
public abstract class StateDecision<SC> where SC : StateController
{
public State<SC> TrueState;
}
public class KeyPressedDecision : StateDecision<StateController>
{
}
答案 0 :(得分:2)
错误是正确的。我想你需要查看以下关于协方差和逆变的文章。
https://msdn.microsoft.com/en-us/library/dd799517(v=vs.110).aspx
您可以通过使用界面实现您想要的效果。