/*
一个人吃食物,只要吃东西就会增加0.6KG 如果出门遛弯没走100步就会减少0.2KG,不到100步就不计算,请按照面向对象的思想编程
*/
#import <Foundation/Foundation.h>
@interface Person :NSObject
{
@public
int _age;
float _weight;
}
-(void)eat:(int) x;
-(void)walk:(int) y;
// 用逻辑值定义方法保存是否吃饭;
@end
@implementation Person
-(void)eat:(int)x{
if (0!=x) {
_weight += 0.6;
}
}
-(void)walk:(int)y{
if (y>100) {
_weight += (y/100+0.2f);
}
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *sp = [Person new];
sp->_weight = 60.0f;//假设他是60kg;
BOOL eatSth = YES;
[sp eat:eatSth];
NSLog(@"他吃了饭所以他体重现在:%.2f",sp->_weight);
[sp walk:200];
NSLog(@"他遛了弯体重减轻到:%.2f",sp->_weight);
}
return 0;
}
|
|