f(sixleaves) = sixleaves

重剑无锋 大巧不工

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  95 随笔 :: 0 文章 :: 7 评论 :: 0 Trackbacks
main.m

 1 //
 2 //  main.m
 3 //  便利构造器与autorelease
 4 //
 5 //  Created by sixleaves on 15/5/10.
 6 //  Copyright (c) 2015年 itcast. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 #import "Person.h"
11 #import "GoodPerson.h"
12 int main(int argc, const char * argv[]) {
13     @autoreleasepool {
14         
15         
16         Person *p1 = [Person person];
17         p1.age = 100;
18         
19         GoodPerson * p2 = [GoodPerson personWithAge: 150];
20         
21         // p2实际上指向的是Person
22         p2.money = 300;
23         
24         //NSLog(@"test");
25         
26         /*
27          代码分析:
28          GoodPerson类调用了personWithAge,所以根据谁调用我,self就指向谁这个原则。
29          self指向GoodPerson类。GoodPerson中没有这个方法,就只能到父类中找,到父类
30          中找到了又调用[self person],GoodPerson中又没有这个方法,于是又找父类,找到后,创建是已经autorelease的Person对象。而不是GoodPerson对象。此时我们只需改掉person方法中写死的[Person alloc]换成self alloc就可以,为什么呢。因为self此时指向的是子类,所以创建出来的就是子类。
31          
32          对于GoodPerson,并没有重写自己的dealloc方法,所以调用的时候,在GoodPerson中找不到,就去其父类Person中找,找到了后,调用了Person得dealloc方法。
33          */
34         
35 
36     }
37     return 0;
38 }
39 
40 /*
41  便利构造器的概念:
42  1.开发中,我们经常会提供一些类方法,快速创建一个已经autorelease的对象。(便利构造器\便利构造方法)
43  便利构造器与self:
44  1.在便利构造器中,尽量使用self代替类名。(或者self class).这能让子类调用时候创造出来的就是子类对象。
45  */
46 

Person.h
 1 //
 2 //  Person.h
 3 //  便利构造器与autorelease
 4 //
 5 //  Created by sixleaves on 15/5/10.
 6 //  Copyright (c) 2015年 itcast. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @interface Person : NSObject
12 
13 @property (nonatomic, assign) int age;
14 
15 // 遍历构造器
16 
17 + (id)person;
18 
19 + (id)personWithAge:(int)age;
20 
21 @end
22 
Person.m
 1 //
 2 //  Person.m
 3 //  便利构造器与autorelease
 4 //
 5 //  Created by sixleaves on 15/5/10.
 6 //  Copyright (c) 2015年 itcast. All rights reserved.
 7 //
 8 
 9 #import "Person.h"
10 
11 @implementation Person
12 
13 // 便利构造器与autorelease
14 /*
15  便利构造器的规范:
16  1.必须是类方法,
17  2.返回类型是id
18  3.必须以类名With开头(带参数一般还要有With),区别之一是便利构造器必须是小写骆驼峰,而类名是大写的。
19  
20  */
21 
22 + (id)person
23 {
24     // return [[[Person alloc] init] autorelease]会导致子类调用personWithAge、person创建的都是已经autorelease的Person对象
25     // 而不是想要的子类对象。换成self就可解决,具体分析看代码分析。
26     return [[[self alloc] init] autorelease];
27 }
28 
29 + (id)personWithAge:(int)age
30 {
31     Person *p = [self person];
32     p.age = age;
33     return p;
34 }
35 
36 - (void)dealloc
37 {
38     NSLog(@"%d岁的对象被销毁了~~~", _age);
39     [super dealloc];
40 }
41 
42 @end
43 

GoodPerson.h
 1 //
 2 //  GoodPerson.h
 3 //  便利构造器与autorelease
 4 //
 5 //  Created by sixleaves on 15/5/10.
 6 //  Copyright (c) 2015年 itcast. All rights reserved.
 7 //
 8 
 9 #import "Person.h"
10 
11 @interface GoodPerson : Person
12 
13 @property (nonatomic, assign) int money;
14 
15 @end
16 
GoodPerson.m 1 //
 2 //  GoodPerson.m
 3 //  便利构造器与autorelease
 4 //
 5 //  Created by sixleaves on 15/5/10.
 6 //  Copyright (c) 2015年 itcast. All rights reserved.
 7 //
 8 
 9 #import "GoodPerson.h"
10 
11 @implementation GoodPerson
12 
13 - (void)dealloc
14 {
15     
16     NSLog(@"GoodPerson-dealloc");
17     [super dealloc];
18 }
19 
20 @end
21 
posted on 2015-05-10 19:35 swp 阅读(198) 评论(0)  编辑 收藏 引用 所属分类: objective-c

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理