- #import <Foundation/Foundation.h>
- @interface Person:NSObject
- {
- @public
- int age;
- double weight;
- }
- - (void)eat;
- - (void)walk;
- @end
- @implementation Person
- - (void)eat
- {
- static int i=0;
- i++;
- weight+=0.6;
- NSLog(@"吃第%d次,体重为%f公斤",i,weight);
- }
- - (void)walk
- {
- static int j=0;
- j++;
- weight-=0.2;
- NSLog(@"走第%d百步,体重为%f公斤",j,weight);
- }
- @end
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
-
- Person *p=[Person new];
- //给成员变量赋值
- p->weight=80;
-
- //重复进行走路动作,定义变量储存步数,
- int steps=600;
-
- for (int i=0; i<steps; i++) {
- if (i%100==0) {
- [p walk];
- [p eat];
- }
- }
-
- }
- return 0;
- }
复制代码 |
|