给定下面的代码,宇宙飞船(程序中只有一个)会检查它是否可以支付新零件的费用,然后“如果(是)”就建造它。发送消息时payFor:
,应该像下面那样处理 iVar,还是应该像每个事务一样使用方法+(void)deduct: (ShipObject *) cost;
,或者我应该使用struct
和使用结构算术?
@ implementation Spaceship
+(void) payFor: (ShipObject) *shipObject
{
totalEnergy -= [shipObject energy];
totalCredits -= [shipObject credits];
totalSamples -= [shipObject samples];
}
+(void) buildShipObject: (ShipObject) *shipObject
{
if ([self canBuild: shipObject]) {
[self payFor: shipObject];
...
}
感谢您的任何见解!