年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2646|回复: 0

将autorelease写入类方法的例子

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-3-19 23:19:10 | 显示全部楼层 |阅读模式
    1、出发点:
    在autoreleasepool中如:
    @autoreleasepool {
            Person * p = [[[Person alloc] init] autorelease];
            ......
    }
    红色部分代码经常需要重复性写,所以需要想个办法将该代码省略掉或替换掉,因此才有了使用一个类方法来返回初始化并autorelease的方法来解决该问题
    + (id)person
    {
            return [[[Person alloc] init] autorelease];
    }
    然后在创建对象时,只需要Person * p = [Person person];即可

    2、既然通过一个类方法可以完成上述功能,在此基础之上其实还可以完成更多的功能,比如顺便传递一个参数进去进行初始化对象,类似初始化方法能完成的功能;
    + (id)personWithAge:(int)age
    {
            Person * p = [Person person];
            p.age = age;
            return p;
    }

    这样一来即完成了初始化+autorelease并将age初始值传到创建的对象中

    3、考虑到子类也需要用到此方法,可以将self替换掉Person更为严谨,否则子类只能重写该方法;

    参考代码:

    Person.h
    1. //
    2. //  Person.h
    3. //  Autorelease-1
    4. //
    5. //  Created by yusian on 14-3-19.
    6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. @interface Person : NSObject
    10. @property (nonatomic, assign) int age;
    11. // 声明一个带自动释放功能的类方法
    12. + (id)person;
    13. // 声明一个带自动释放且传age参数的类方法
    14. + (id)personWithAge:(int)age;
    15. @end
    复制代码
    Person.m
    1. //
    2. //  Person.m
    3. //  Autorelease-1
    4. //
    5. //  Created by yusian on 14-3-19.
    6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    7. //
    8. #import "Person.h"
    9. @implementation Person
    10. + (id)person
    11. {
    12.     //  直接返回带初始化与autorelease的对象,省去在创建方法时重复的代码
    13.     return [[[self alloc] init] autorelease];
    14. }
    15. // 对类方法person进行升级改造,不仅可完成初始化与自动释放,还可以接受一个age的参数给新对象,扩展了原始方法的功能
    16. + (id)personWithAge:(int)age
    17. {
    18.     // 创建一个对象并调用person方法完成初始化与自动释放的功能,考虑到子类的使用,用self表示当前类的行为
    19.     // 这需要与person方法结果,person方法同样用self来表示当前类
    20.     Person * p = [self person];
    21.     // 如果子类继承该方法,由于"多态"的特性,创建一个Person类型的指针,指向一个Student类型的对象是完全没有问题的
    22.    
    23.     // 对象分配空间并初始化之后将该对象的age赋值
    24.     p.age = age;
    25.    
    26.     // 返回该对象为方法调用者
    27.     return p;
    28. }
    29. - (void)dealloc
    30. {
    31.     // 在释放之前输出一行信息,直观表示该方法被调用,使用对象本身与_age来区分具体对象
    32.     NSLog(@"%@ is dealloc, age = %d", self, _age);
    33.    
    34.     [super dealloc];
    35. }
    36. @end
    复制代码
    Student.h
    1. //
    2. //  Student.h
    3. //  Autorelease-1
    4. //
    5. //  Created by yusian on 14-3-19.
    6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    7. //
    8. #import "Person.h"
    9. @interface Student : Person
    10. @property (nonatomic, assign) double height;
    11. @end
    复制代码
    Student.m
    1. //
    2. //  Student.m
    3. //  Autorelease-1
    4. //
    5. //  Created by yusian on 14-3-19.
    6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    7. //
    8. #import "Student.h"
    9. @implementation Student
    10. @end
    复制代码
    main.m
    1. //
    2. //  main.m
    3. //  Autorelease-1
    4. //
    5. //  Created by yusian on 14-3-19.
    6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. #import "student.h"
    10. int main()
    11. {
    12.     // 创建一个自动释放池
    13.     @autoreleasepool{
    14.         
    15.         // Person * p = [Person person];
    16.         
    17.         // 创建一个Person对象并使用personWithAge:方法进行初始化,检验自动释放池被释放后该对象是否会自动释放
    18.         Person * p = [Person personWithAge:20];
    19.         
    20.         // 创建一个Person子类Student对象,子类继承父类的功能,是否能够达到同样的效果
    21.         Student * s = [Student personWithAge:12];
    22.         
    23.         // 创建对象成功的标志,即对象可对新成员变量(父类中没有的)进行操作
    24.         s.height = 173;
    25.         
    26.         NSLog(@"s.height = %.2f", s.height);
    27.         
    28.     }
    29.    
    30.     return 0;
    31. }
    复制代码
    运行结果:
    2014-03-19 23:05:36.560 Autorelease-1[2617:303] s.height = 173.00
    2014-03-19 23:05:36.561 Autorelease-1[2617:303] <Student: 0x100107b60> is dealloc, age = 12
    2014-03-19 23:05:36.562 Autorelease-1[2617:303] <Person: 0x100102fe0> is dealloc, age = 20
    Program ended with exit code: 0



    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-5 23:16 , Processed in 0.043983 second(s), 19 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表