.h文件。。。。。。。
@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文件
@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
本文出自 “小刘_Blog” 博客,请务必保留此出处http://liuyafang.blog.51cto.com/8837978/1543816
对不同类型assgin,retain,和copy内部实现的方法
原文地址:http://liuyafang.blog.51cto.com/8837978/1543816