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

.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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值