// // main.m // ATM // #import <Foundation/Foundation.h> #import "ATM.h" #import "Money.h" #import "BankCard.h" int main(int argc, const char * argv[]) { ATM * atm = [[ATM alloc]init]; //atm->_saveMoneyCount = 1000; BankCard * card = [BankCard alloc]; // [card set_cardNumber:@"12345678"]; // [card set_userName:@"张三"]; [atm tellMoney:TypeOfMoneyFake]; [card showCardNumber:@"12345678"]; [card showUserName:@"李四"]; NSLog(@"%@",card); [atm saveMoney:10000]; NSLog(@"%@",atm); [atm getMoney:5000]; NSLog(@"%@",atm); return 0; }
#import <Foundation/Foundation.h> #import "BankCard.h" #import "Money.h" @interface ATM : NSObject { //用来记录钱的总数 int _countOfMoney; //存钱数目 int _saveMoneyCount; //取钱数目 int _getMoneyCount; } @property int _countOfMoney; @property int _saveMoneyCount; @property int _getMoneyCount; -(void)saveMoney:(int)saveMoney; -(void)getMoney:(int)getMoney; -(void)tellMoney:(TypeOfMoney)typeOfMoney; @end
#import "ATM.h" @implementation ATM @synthesize _countOfMoney = countOfMoney; @synthesize _saveMoneyCount = saveMoneyCount; @synthesize _getMoneyCount = getMoneyCount; -(void)saveMoney:(int)saveMoney { //这里参数名称的命名最好不好和实例变量的名称相同 //如果这样做会发生将实例变量隐藏 不影响运行,但是会有警告 _saveMoneyCount = saveMoney; _countOfMoney = _saveMoneyCount; NSLog(@"%d",_saveMoneyCount); } -(void)getMoney:(int)getMoney { _getMoneyCount = getMoney; _saveMoneyCount -= getMoneyCount; NSLog(@"%d",_getMoneyCount); } -(void)tellMoney:(TypeOfMoney)typeOfMoney { switch (typeOfMoney) { case TypeOfMoneyReal: NSLog(@"真币"); break; case TypeOfMoneyFake: NSLog(@"假币"); break; default: break; } } -(NSString *)description { return [NSString stringWithFormat:@"银行卡结余=%d 存入钱数=%d 取出钱数=%d ",_countOfMoney,_saveMoneyCount,_getMoneyCount]; } @end
#import <Foundation/Foundation.h> enum TypeOfMoney { TypeOfMoneyReal,//真币 TypeOfMoneyFake//假币 }; typedef enum TypeOfMoney TypeOfMoney; @interface Money : NSObject @end
#import "Money.h" @implementation Money { } @end
#import <Foundation/Foundation.h> @interface BankCard : NSObject { /**用来存储银行卡账号*/ NSString * _cardNumber; /**用来存储银行卡使用者姓名*/ NSString * _userName; } -(void)showCardNumber:(NSString *)number; -(void)showUserName:(NSString *)name; @property NSString * _cardNumber; @property NSString * _userName; @end
#import "BankCard.h" @implementation BankCard @synthesize _cardNumber = carNumber; @synthesize _userName = userName; -(void)showCardNumber:(NSString *)number { //这里忘记了传参数!!! _cardNumber = number; NSLog(@"%@",_cardNumber); } -(void)showUserName:(NSString *)name { _userName = name; NSLog(@"%@",_userName); } -(NSString *)description { return [NSString stringWithFormat:@"账号:%@ 用户名:%@",_cardNumber,_userName]; } @end