温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-27 16:45:27 来源:网络 阅读:611 作者:Im刘亚芳 栏目:开发技术

.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



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI