我的Account
应用程序中有一个班级,该班级是用户的银行帐户。初始化称为Withdrawals
和的两个类Deposits
。他们看起来像这样:
帐号h
@interface Account : NSObject
@property (nonatomic, copy) NSInteger *amount;
@property (nonatomic, strong) Withdrawal *withdrawal;
@property (nonatomic, strong) Deposit *deposit;
- (id)initWithAmount:(NSInteger *)amount;
- (Withdrawal *)withdrawal;
- (Deposit *)deposit;
@end
帐号
@implementation Account
- (id)initWithAmount:(NSInteger *)amount {
self = [super init];
if (self)
{
_amount = amount;
_withdrawal = [[Withdrawal alloc] init];
_deposit = [[Deposit alloc] init];
}
return self;
}
- (Withdrawal *)withdrawal {
return _withdrawal;
}
- (Deposit *)deposit {
return _deposit;
}
@end
理想情况下,我希望能够打电话[[account withdrawal] withdraw:50]
并[account amount]
进行更新。解决此问题的最佳方法是什么?
首先,它amount
应该不太可能具有type NSInteger *
,即是一个指向整数的指针,它更有可能应该简单地是NSInteger
,它是一个整数。的所有其他用法也一样NSInteger *
。这是因为amount
是一个值,而不是对对象的引用,这不同于说youwithdrawal
属性返回对对象的引用。
理想情况下,我希望能够打电话
[[account withdrawal] withdraw:50]
并[account amount]
进行更新。解决此问题的最佳方法是什么?
在不对设计发表评论的情况下,如果提款对象需要访问您的帐户对象,则它需要一个(获取方法)引用。您应该从Withdrawal
类的角度考虑其关联的属性Account
,就像您Account
具有其关联的属性一样Withdrawal
。例如,您可能在创建Withdrawal
对象时设置此设置,其中您的当前设置为:
_withdrawal = [[Withdrawal alloc] init];
变成:
_withdrawal = [[Withdrawal alloc] initWithAccount:self];
这样做可能会导致您创建一个循环-每个Account
实例都引用一个Withdrawal
实例,而后者又引用了该Account
实例。循环本身并不坏,只有在阻止不想要的对象被收集时它们才会变坏。但是,我怀疑您Account
最终会使用一种closeAccount
方法,在该方法中,您可以根据需要中断任何循环。
希望这会给您带来一些帮助,并继续努力。如果您发现设计/代码不起作用,请提出一个新问题,详细说明您的设计和编码以及问题所在。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句