合同由IAction
州定义,您可以Perform
对任何行动采取行动IActor
.任何 IActor.不仅如此IHealthUser
.您HealthPotion
尝试做的只是实现一个子集IAction
,意味着仅在IActor
s 的子集上执行其任务.这不是IAction
界面所说的.
如果您希望特定IAction
于能够限制IActor
其适用的类型,则可以使用通用约束来执行此操作:
public interface IActionwhere TAppliesTo : IActor { void Perform(TAppliesTo appliesTo); } public class UniversalAction : IAction { public void Perform (IActor anyone) {} } public class HealthPotion : IAction { public void Perform (IHealthUser healthUserOnly){} }