一个人可以吃不同的食物,只要吃东西就会增加体重0.6,如果 要是出门遛弯,每走100步,体重减0.2,小于100步忽略不计。 请用面向对象思想实现。
思路;
类名:Person
属性:年龄(_age)、体重(_weight)
动作:吃饭(eat)方法、散步(walk)方法
#import <Foundation/Foundation.h>
//类的声明.规定当前类的 类名,属性,行为
@interface Person :NSObject
{ @public
int _age;
float _weight;
}
//类的行为.方法
-(void)eat;
-(void)walk:(int)buShu;
@end
//类的实现.实现行为
@implementation Person
-(void)eat{
_weight+=0.6;
NSLog(@"吃吃吃,就知道吃!吃死你啊~肥死你吃完体重%.2f",_weight);
};
-(void)walk:(int)buShu{
float tizhong =_weight - (buShu/100)*0.2;
NSLog(@"嘿哈嘿哈嘿哈......好累啊....跑完减肥%.2f",tizhong);
}
@end
int main(){
@autoreleasepool{
Person *per1 = [Person new];
per1->_age = 20;
per1->_weight = 90;
[per1 eat];
[per1 walk:600];
}
return 0;
}
|
|