#import<Foundation/Foundation.h>
//创建一个人账户类.有一个属性-余额
@interface Account:NSObject{
int _yuE;//不允许获取这个余额
}
-(void)setYuE:(int) yuE;
+(void)printBank;
-(void)quQian:(int)money;//需要有一个取钱的方法.
-(void)printYuE;//定义一个方法打印这个账户信息打印余额
@end
@implementation Account
-(void)setYuE:(int) yuE{
if(_yuE<=0){
NSLog(@"好贼,给我负的?!");
}else{
_yuE+=yuE;//累加
}
}
+(void)printBank{
NSLog(@"欢迎光临中国银行");
}
-(void)quQian:(int)money{
NSLog(@"请输入你要取的钱");
if(money<=0){
NSLog(@"滚");
}else if(money>_yuE){
NSLog(@"滚开");
}else{
NSLog(@"请收好您的钱:%d",money);
_yuE-=money;
}
}
-(void)printYuE{
NSLog(@"当前余额为%d",_yuE);
}
@end
//定义一个银行类.里面有两个个人账户.
@interface Bank:NSObject{
Account* _a1;
Account* _a2;
}
-(void)setA1:(Account*) a1;
-(Account*)getA1;
-(void)setA2:(Account*) a2;
-(Account*)getA2;
-(void)printAllAccount;
@end
@implementation Bank
-(void)setA1:(Account*) a1{
_a1 = a1;
}
-(Account*)getA1{
return _a1;
}
-(void)setA2:(Account*) a2{
_a2 = a2;
}
-(Account*)getA2{
return _a2;
}
-(void)printAllAccount{
[_a1 printYuE];
[_a2 printYuE];
}
@end
int main(){
//请打印这个银行类中所有账户的信息
Bank* bank = [Bank new];
Account* ds = [Account new];
[ds setYuE:100];
Account* th = [Account new];
[th setYuE:1000000];
[bank setA1:ds];
[bank setA2:th];
[bank printAllAccount];
[[bank getA1]quQian:100];
[[bank getA2]quQian:1000];
[bank printAllAccount];
return 0;
}
哪位大神有空帮忙看看 |
|