zoukankan      html  css  js  c++  java
  • oc基础复习03-OC的类02参数

    先写一个Person类 Person.h

     1 #import <Foundation/Foundation.h>
     2 
     3 @interface Person : NSObject
     4 {
     5 @public
     6     int age;
     7     double height;
     8 }
     9 - (void)print;
    10 
    11 @end

    Person.m

    1 #import "Person.h"
    2 
    3 @implementation Person
    4 - (void)print
    5 {
    6     NSLog(@"年龄=%d,身高=%f", age, height);
    7 }
    8 @end

    main.m

     1 #import <Foundation/Foundation.h>
     2 #import "Person.h"
     3 void test1(int newAge, double newHeight);
     4 void test2(Person *newP);
     5 void test3(Person *newP);
     6 void test4(Person *newP);
     7 
     8 int main(int argc, const char * argv[]) {
     9     @autoreleasepool {
    10         Person *p = [Person new];
    11         p->age = 10;
    12         p->height = 1.5f;
    13         
    14         test1(p->age, p->height);
    15         [p print]; //10  1.5
    16         
    17         test2(p);// 20 1.7
    18         [p print];
    19         NSLog(@"before = aft%@",p);
    20         test3(p);//20 1.7
    21         [p print];
    22         NSLog(@"after = %@",p);
    23         
    24         test4(p);//60 1.9
    25         [p print];
    26         
    27     }
    28     return 0;
    29 }
    30 
    31 void test1(int newAge, double newHeight)
    32 {
    33     newAge = 10;
    34     newHeight = 1.6;
    35 }
    36 
    37 void test2(Person *newP)
    38 {
    39     newP->age = 20;
    40     newP->height = 1.7;
    41 }
    42 
    43 void test3(Person *newP)
    44 {
    45     NSLog(@"刚传入参数地址newP = %@", newP);
    46     Person *p2 = [Person new];//p2指向一个新的存储空间
    47     p2->age = 40;
    48     p2->height = 1.8;
    49     newP = p2;//将p2的新的储存空间指向newP 这里更改只是更改p2里面的内容 而不会对newP 原来指向的地址进行更改
    50     NSLog(@"newP 更改后的地址= %@", newP);
    51     newP->age = 30;
    52 }
    53 
    54 void test4(Person *newP)
    55 {
    56     Person *p2 = newP; //将newp的地址赋值给p2 然后进行更改p2的内容 其实就相当于更改newP里面的内容
    57     p2->age = 50;
    58     p2->height = 1.9;
    59     newP->age = 60;
    60 }

    运行结果:

    2015-06-30 11:11:43.933 test[40776:1124334] 年龄=10,身高=1.500000

    2015-06-30 11:11:43.934 test[40776:1124334] 年龄=20,身高=1.700000

    2015-06-30 11:11:43.934 test[40776:1124334] before = aft<Person: 0x100211ba0>

    2015-06-30 11:11:43.935 test[40776:1124334] 刚传入参数地址newP = <Person: 0x100211ba0>

    2015-06-30 11:11:43.935 test[40776:1124334] newP 更改后的地址= <Person: 0x100100330>

    2015-06-30 11:11:43.935 test[40776:1124334] 年龄=20,身高=1.700000

    2015-06-30 11:11:43.935 test[40776:1124334] after = <Person: 0x100211ba0>

    2015-06-30 11:11:43.935 test[40776:1124334] 年龄=60,身高=1.900000

  • 相关阅读:
    菜单导航组件
    flask+nginx+uwsgi在服务器搭建项目
    安装Anaconda
    vscode上eslink包失效
    js滚动事件
    打字游戏
    js更高文档的样式
    js事件
    Dom对象更改文档结构.html
    js重点
  • 原文地址:https://www.cnblogs.com/greenboy/p/4609851.html
Copyright © 2011-2022 走看看