对不同类型assgin,retain,和copy内部实现的方法

来源:互联网 发布:软件项目经理资质证书 编辑:程序博客网 时间:2024/06/11 03:39

.h文件。。。。。。。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@interface book : NSObject
/*{
   
    NSString *_bookName;    //书名
    CGFloat _bookThickness; //厚度
    NSString *_bookType;      //书类型
    NSInteger _bookPrice;     //书价格
    NSString *_publishingHouse;//出版社
    NSString *_publishintTime;  //出版时间
}*/  //在@property里面,其实就包含了定义实例变量,setter方法和getter方法。这里可以不用在定义实例变量了
@property (nonatomic , copy)NSString *bookName;//用copy写完整的属性
@property (nonatomic , assign)CGFloat bookThickness;
@property (nonatomic , retain)NSString *bookType;//retain不建议使用,大多使用copy
@property (nonatomic , assign)NSInteger bookPrice;
@property (nonatomic , copy)NSString *publishingHouse;
@property (nonatomic , copy)NSString *publishintTime;
- (void)read;
- (void)write;
@end


.m文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
@implementation book
@synthesize bookName = _bookName ;
@synthesize bookThickness = _bookThickness;
@synthesize bookType = _bookType;
@synthesize bookPrice = _bookPrice;
@synthesize publishingHouse = _publishingHouse;
@synthesize publishintTime = _publishintTime;
- (void)setBookName:(NSString *)bookName
{
    if(_bookName != bookName) {
        [_bookName release];//auto---All---Combined---Language--Objective C--->no这里是对内存的一个设置,
        _bookName = [bookName copy];
    }
}
- (NSString *)bookName
{
    return [[_bookName retain] autorelease];
}
- (void)setBookThickness:(CGFloat)bookThickness
{
    _bookThickness = bookThickness;
}
- (CGFloat)bookThickness
{
    return_bookThickness;
}
- (void)setBookType:(NSString *)bookType
{
    if(_bookType != bookType) {
        [_bookType release];
        _bookType = [bookType retain];//retain和copy唯一的不同在这里。
    }
}
- (NSString *)bookType
{
    return[[_bookType retain] autorelease];
}
- (void)setBookPrice:(NSInteger)bookPrice
{
    _bookPrice = bookPrice;
}
- (NSInteger)bookPrice
{
    return_bookPrice;
}
- (void)setPublishingHouse:(NSString *)publishingHouse
{
    if(_publishingHouse != publishingHouse) {
        [_publishingHouse release];
        _publishingHouse = [publishingHouse copy];
    }
}
- (NSString *)publishingHouse
{
    return[[_publishingHouse retain] autorelease];
}
- (void)setPublishintTime:(NSString *)publishintTime
{
    if(_publishintTime != publishintTime) {
        [_publishintTime release];
        _publishintTime = [publishintTime copy];
    }
}
- (NSString *)publishintTime
{
    return [[_publishintTime retain] autorelease];
}
- (void)read;
{
    NSLog(@"这是一本书");
}
- (void)write
{
    NSLog(@"可以写");
}
@end

 

0 0