简体   繁体   English

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

[英]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 我希望能够做到

  • WolfState that uses WolfController inheriting from State 使用WolfController从State继承的WolfState
  • SheepState that uses SheepController and inherits from State SheepState使用SheepController并从State继承

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM