| 一个人可以吃不同的食物,只要吃东西就会增加体重0.6,如果 要是出门遛弯,每走100步,体重减0.2,小于100步忽略不计。 请用面向对象思想实现。 思路; 类名:Person  属性:年龄(_age)、体重(_weight) 动作:吃饭(eat)方法、散步(walk)方法 复制代码#import<Foundation/Foundation.h>
@interface Person : NSObject {
    @public
    int _age;////数据类型  _实例变量名
    float _weight;
}
-(float) eat:(int)x ;//有参方法的声明
-(float) walk:(int)x;
@end
@implementation Person
//方法名 eat:
- (float) eat:(int)x{
    //只要吃东西就会增加体重0.6,
    _weight+=0.6*x;
        return _weight;
    }
//方法名 walk:
    -(float)walk:(int)x{
        //每走100步,体重减0.2,小于100步忽略不计。
        if(x>100)_weight-=0.2*x;
        return _weight;
            
    }
@end
int main(){
    @autoreleasepool {
        //创建对象
        Person *person= [Person new];
        //对象访问实例变量
        person->_weight=100;
        //调用有参的方法
        int eatWeight=[person eat:1];
        int walkWeight=[person walk:2];
        NSLog(@"eatWeight=%f,walkWeight=%f",eatWeight,walkWeight);
    }
    return 0;
}
 
 |