- /*
- 人
- 类名:person
- 属性:(实例变量\成员变量):体重,年龄
- 行为(方法):走路
-
- */
- #import <Foundation/Foundation.h>
- @interface Person : NSObject
- {
- @public
- int age;
- double weight;
- }
- - (void)walk;
- @end
- @implementation Person
- - (void)walk
- {
- NSLog(@"走四方哦路迢迢水长长~");
- //NSLog(@"此人%d岁,重%d千克",age,weight);此处输出格式有误
- NSLog(@"此人%d岁,重%f千克",age,weight);
- }
- @end
- int main(int argc, const char * argv[])
- {
- Person *p1 = [Person new];
- p1->age = 17;
- p1->weight = 56.0;
- //[p1 walk];
- Person *p2 = [Person new];
- p2->age = 29;
- p2->weight = 68.0;
-
- //指针p1现在指向的是 p2的地址 了
- p1 = p2;
- //只修改age
- p1->age = 22;
- //调用方法
- [p1 walk];
- // NSLog(@"此人%d岁,重%d千克",p1->age,p1->weight);
-
- @autoreleasepool {
-
- // insert code here...
- NSLog(@"Hello, World!");
-
- }
- return 0;
- }
- /*
- 2014-03-22 12:31:23.586 oc-4 成员与成员变量[7809:303] 走四方哦路迢迢水长长~
- 2014-03-22 12:31:23.588 oc-4 成员与成员变量[7809:303] 此人22岁,重22千克
- 2014-03-22 12:31:23.588 oc-4 成员与成员变量[7809:303] Hello, World!
- Program ended with exit code: 0
-
- 疑问:输出的为啥不是22岁,重68千克
- */
- //把27行的%d改成%f,你试试?
复制代码 |