Type typeThing = gumballMachine.GetState().GetType();
if (typeThing == typeof(NoQuarterState)) { ... }
IState state;
public IState GetState() {
return state;
}
public class NoQuarterState : IState { ... }
我使用头部第一设计模式Ch10中的gumballMachine应用程序。 full code here这工作。GetType和typeof
问题:typeThing上面的气味!有没有更好的办法?
我不知道任何其他方式获得的类型实例,除了GetType()。有些检查可以通过'is'和'as'运算符来确定它是否是你想要的类型。 –