溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

對不同類型assgin,retain,和copy內(nèi)部實(shí)現(xiàn)的方法

發(fā)布時間:2020-07-27 16:45:27 來源:網(wǎng)絡(luò) 閱讀:615 作者:Im劉亞芳 欄目:開發(fā)技術(shù)

.h文件。。。。。。。

@interface book : NSObject
/*{
    
    NSString *_bookName;    //書名
    CGFloat _bookThickness;  //厚度
    NSString *_bookType;      //書類型
    NSInteger _bookPrice;     //書價(jià)格
    NSString *_publishingHouse;//出版社
    NSString *_publishintTime;  //出版時間
}*/  //在@property里面,其實(shí)就包含了定義實(shí)例變量,setter方法和getter方法。這里可以不用在定義實(shí)例變量了 
@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這里是對內(nèi)存的一個設(shè)置,
        _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問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI