练习题
代码实现
#import <Foundation/Foundation.h>
@interface Person:NSObject{
@public
NSString *name;
int _age;
float _weight;
}
-(void)eat:(NSString *)foodName ;
-(void)walk:(int)num;
@end
@implementation Person
//每吃食物,增加0.6
-(void)eat:(NSString *)foodName{
_weight += 0.6f;
NSLog(@"%@ has eat %@, your weight is %f",name,foodName, _weight);
}
//每走一百步,体重减少0.2
-(void)walk:(int)num{
_weight-=100/num * 0.2f;
NSLog(@"%@ has walk %d, your weight is %f",name, num, _weight);
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
//
Person *person = [Person new];
person->name = @"tom";
person->_age = 22;
person->_weight = 120.0f;
[person eat:@"apple"];
[person walk:50];
//NSLog(@"Hello, World!");
}
return 0;
}