#import <Foundation/Foundation.h>
@interface Person:NSObject{
NSString *_name;
float _weight;
}
-(void) create:(NSString *)name :(float)weight;
-(void) eat:(int) cnt;
-(void) walk:(int) num;
@end
@implementation Person
-(void) create:(NSString *)name :(float)weight{
_name = name;
_weight = weight;
NSLog(@"创建一个人,名字为%@,体重为%.2f", _name, _weight);
}
-(void) eat:(int) cnt{
_weight += cnt*0.6f;
NSLog(@"%@吃了%d次,体重为%.2f", _name, cnt, _weight);
}
-(void) walk:(int) num{
_weight -= num*0.2f;
NSLog(@"%@走了%d步,体重为%.2f", _name, num, _weight);
}
@end
Person *men, *women;
void creat_person(){
men = [Person new];
[men create:@"one" :130];
women= [Person new];
[women create:@"two" :100];
}
void eat_food(){
[men eat:1];
[women eat:2];
}
void sport(){
[men walk:1];
[women walk:1];
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"创建两个人");
creat_person();
NSLog(@"======吃早餐======");
eat_food();
sport();
NSLog(@"======吃午饭======");
eat_food();
sport();
NSLog(@"======吃晚饭======");
eat_food();
sport();
}
return 0;
}
结果:
2016-11-19 11:01:41.996 fisrt[923:16956] 创建两个人
2016-11-19 11:01:41.999 fisrt[923:16956] 创建一个人,名字为one,体重为130.00
2016-11-19 11:01:42.000 fisrt[923:16956] 创建一个人,名字为two,体重为100.00
2016-11-19 11:01:42.001 fisrt[923:16956] ======吃早餐======
2016-11-19 11:01:42.002 fisrt[923:16956] one吃了1次,体重为130.60
2016-11-19 11:01:42.003 fisrt[923:16956] two吃了2次,体重为101.20
2016-11-19 11:01:42.003 fisrt[923:16956] one走了1步,体重为130.40
2016-11-19 11:01:42.004 fisrt[923:16956] two走了1步,体重为101.00
2016-11-19 11:01:42.005 fisrt[923:16956] ======吃午饭======
2016-11-19 11:01:42.006 fisrt[923:16956] one吃了1次,体重为131.00
2016-11-19 11:01:42.006 fisrt[923:16956] two吃了2次,体重为102.20
2016-11-19 11:01:42.007 fisrt[923:16956] one走了1步,体重为130.80
2016-11-19 11:01:42.008 fisrt[923:16956] two走了1步,体重为102.00
2016-11-19 11:01:42.008 fisrt[923:16956] ======吃晚饭======
2016-11-19 11:01:42.008 fisrt[923:16956] one吃了1次,体重为131.40
2016-11-19 11:01:42.009 fisrt[923:16956] two吃了2次,体重为103.20
2016-11-19 11:01:42.010 fisrt[923:16956] one走了1步,体重为131.20
2016-11-19 11:01:42.011 fisrt[923:16956] two走了1步,体重为103.00
Program ended with exit code: 0