我的應用程序中有一個Account
類,它是用戶的銀行帳戶。這會初始化兩個類,稱爲Withdrawals
和Deposits
。他們是這樣的:從類到父類的通信?
Account.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
Account.m
@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]
也會被更新。解決這個問題的最好方法是什麼?
@vadian你是對的。我重寫了示例以更好地匹配我的應用中的實際代碼。 – user4992124