/*
类名:Person
属性:年龄(_age) 体重(_weight)
动作:吃饭方法(eat) 散步方法(walk)
功能:人可以吃各种食物
每次散步100步让体重减0.2,不足100步不做计算
*/
#import <Foundation/Foundation.h>
//类的声明
@interface Person:NSObject
{
@public
//类的属性 类的成员变量 类的实例变量
int _age;
float _weight;
NSString *_name;
}
//类的方法
-(void)eat:(NSString *) foodName;
-(void)walk:(int) num;
@end
//类的方法多实现
@implementation Person
//吃 方法的实现
-(void)eat:(NSString *)foodName{
NSLog(@"人吃的食物是:%@",foodName);
_weight += 0.6;
NSLog(@"吃饭后体重是:%.2f",_weight);
}
// 散步 方法的实现
-(void)walk:(int)num{
NSLog(@"人走路的步数是:%d",num);
_weight -= num/100*0.2;
NSLog(@"人走了%d步后体重是:%.2f",num,_weight);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *perOne = [Person new];
perOne->_weight = 98.3f;
perOne->_age = 25;
perOne->_name = @"张三丰";
NSLog(@"人吃饭前的重量是:%.2f",perOne->_weight);
[perOne eat:@"蛋炒饭"];
[perOne eat:@"蛋炒饭"];
[perOne walk:15555];
}
return 0;
}
|
|