日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

4.4OC10-内存管理2-set方法的内存管理

發(fā)布時(shí)間:2023/12/20 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 4.4OC10-内存管理2-set方法的内存管理 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

4.4OC10-內(nèi)存管理2-set方法的內(nèi)存管理

?

例一:

main.m

?//

//? main.m

//? OC10-內(nèi)存管理2-set方法的內(nèi)存管理

//

//? Created by qwz on 13-12-9.

//? Copyright (c) 2013年?renhe. All rights reserved.

//

?

#import?<Foundation/Foundation.h>

#import?"Student.h"

#import?"Book.h"

?

int?main(int?argc,?const?char?* argv[])

{

?

? ??@autoreleasepool?{

? ? ? ??Student?*stu = [[Student?alloc]?initWithAge:29];

? ? ? ??Book?*book = [[Book?alloc]?initWithPrice:3.5];

?? ? ? ?

? ? ? ? [book?release];

? ? ? ? [stu?release];

? ? }

? ??return?0;

}

?

Student.h

?//

//? Student.h

//? OC10-內(nèi)存管理2-set方法的內(nèi)存管理

//

//? Created by liuyes on 13-12-9.

//? Copyright (c) 2013年?renhe. All rights reserved.

//

?

#import?<Foundation/Foundation.h>

#import?"Book.h"

?

@interface?Student :?NSObject

?

@propertyint?age;

?

- (id)initWithAge:(int)age;

?

@propertyBook?*book;

@end

?

Student.m
?//

//? Student.m

//? OC10-內(nèi)存管理2-set方法的內(nèi)存管理

//

//? Created by liuyes on 13-12-9.

//? Copyright (c) 2013年?renhe. All rights reserved.

//

?

#import?"Student.h"

?

@implementation?Student

?

#pragma mark?構(gòu)造方法

- (id)initWithAge:(int)age{

? ??if(?self?= [super?init]){

? ? ? ??_age?= age;

? ? }

? ??returnself;

}

?

#pragma mark?回收對(duì)象

- (void)dealloc{

? ??NSLog(@"student:%i?被銷毀了", _age);

? ? [super?release];

}

?

@end

?

?Book.h
?//

//? Book.h

//? OC10-內(nèi)存管理2-set方法的內(nèi)存管理

//

//? Created by liuyes on 13-12-9.

//? Copyright (c) 2013年?renhe. All rights reserved.

//

?

#import?<Foundation/Foundation.h>

?

@interface?Book :?NSObject

@propertyfloat?price;?//價(jià)格

?

- (id)initWithPrice:(float)price;

?

@end

?

?Book.m
?//

//? Book.m

//? OC10-內(nèi)存管理2-set方法的內(nèi)存管理

//

//? Created by liuyes on 13-12-9.

//? Copyright (c) 2013年?renhe. All rights reserved.

//

?

#import?"Book.h"

?

@implementation?Book

?

- (id)initWithPrice:(float)price{

? ??if?(self?= [super?init]){

? ? ? ??_price?= price;

? ? }

? ??returnself;

}

?

- (void)dealloc{

? ??NSLog(@"book:%f?被銷毀了", _price);

? ? [super?dealloc];

}

?

@end

運(yùn)行結(jié)果

2013-12-09 17:04:29.679 OC10-內(nèi)存管理2-set方法的內(nèi)存管理[634:403] book:3.500000 被銷毀了

2013-12-09 17:04:29.703 OC10-內(nèi)存管理2-set方法的內(nèi)存管理[634:403] student:29 被銷毀了

--------------------------------- ---------------------------------- 例二: main.m
?

//

//? main.m

//? OC10-內(nèi)存管理2-set方法的內(nèi)存管理

//

//? Created by qwz on 13-12-9.

//? Copyright (c) 2013 renhe. All rights reserved.

//

?

#import <Foundation/Foundation.h>

#import "Student.h"

#import "Book.h"

?

void test(Student *stu){

? ? //book:1

? ? Book *book = [[Book alloc] initWithPrice:3.5];

?? ?

? ? //book:2

? ? stu.book = book;

?? ?

? ? //book:1

? ? [book release];

?? ?

? ? Book *book2 = [[Book alloc] initWithPrice:4.5];

? ? //book2:2

? ? stu.book = book2;

? ? //book2:1

? ? [book2 release];

?? ?

}

?

void test1(Student *stu){

? ? [stu readBook];

}

?

int main(int argc, const char * argv[])

{

?

? ? @autoreleasepool {

? ? ? ? //stu:1

? ? ? ? Student *stu = [[Student alloc] initWithAge:29];

?? ? ? ?

? ? ? ? //stu:1

? ? ? ? //book:1

? ? ? ? //book2:1

? ? ? ? test(stu);

?? ? ? ?

? ? ? ? //stu:1

? ? ? ? //book:1

? ? ? ? //book2:1

? ? ? ? test1(stu);

?? ? ? ?

? ? ? ? //stu:0

? ? ? ? //book:0

? ? ? ? [stu release];

? ? }

? ? return 0;

}

?

Student.h
?

//

//? Student.h

//? OC10-內(nèi)存管理2-set方法的內(nèi)存管理

//

//? Created by liuyes on 13-12-9.

//? Copyright (c) 2013 renhe. All rights reserved.

//

?

#import <Foundation/Foundation.h>

#import "Book.h"

?

@interface Student : NSObject{

? ? Book *_book;

}

?

@propertyint age;

?

- (id)initWithAge:(int)age;

?

@propertyBook *book;

?

- (void)readBook;

?

@end

Student.m

?

//

//? Student.m

//? OC10-內(nèi)存管理2-set方法的內(nèi)存管理

//

//? Created by liuyes on 13-12-9.

//? Copyright (c) 2013 renhe. All rights reserved.

//

?

#import "Student.h"

?

@implementation Student

?

#pragma mark 構(gòu)造方法

- (id)initWithAge:(int)age{

? ? if( self = [super init]){

? ? ? ? _age = age;

? ? }

? ? returnself;

}

?

#pragma mark 回收對(duì)象

- (void)dealloc{

? ? //釋放對(duì)象

? ? [_book release];

? ? //[self.book release];

? ? NSLog(@"student:%i 被銷毀了", _age);

? ? [super dealloc];

}

?

#pragma mark - gettersetter方法

//@synthesize book = _book;

//如果自己手動(dòng)實(shí)現(xiàn)了gettersetterxcode就不會(huì)自動(dòng)生成@synthesize

//也就不會(huì)自動(dòng)生成_book

//gettersetter的默認(rèn)實(shí)現(xiàn)

- (void)setBook:(Book *)book{

? ? if(_book != book){

? ? ? ? //先釋放舊的成員變量

? ? ? ? [_book release];

? ? ? ? //retain新傳進(jìn)來的對(duì)象

? ? ? ? _book = [book retain];

? ? }

}

?

- (Book *)book{

? ? return? _book;

}

?

#pragma mark - 公共方法

#pragma mark 讀書

- (void)readBook{

? ? NSLog(@"當(dāng)前讀的書是:%f", _book.price);

}

?

@end

Book.h

?

//

//? Book.h

//? OC10-內(nèi)存管理2-set方法的內(nèi)存管理

//

//? Created by liuyes on 13-12-9.

//? Copyright (c) 2013 renhe. All rights reserved.

//

?

#import <Foundation/Foundation.h>

?

@interface Book : NSObject

@propertyfloat price; //價(jià)格

?

- (id)initWithPrice:(float)price;

?

@end

Book.m?

?

//

//? Book.m

//? OC10-內(nèi)存管理2-set方法的內(nèi)存管理

//

//? Created by liuyes on 13-12-9.

//? Copyright (c) 2013 renhe. All rights reserved.

//

?

#import "Book.h"

?

@implementation Book

?

- (id)initWithPrice:(float)price{

? ? if (self = [super init]){

? ? ? ? _price = price;

? ? }

? ? returnself;

}

?

- (void)dealloc{

? ? NSLog(@"book:%f 被銷毀了", _price);

? ? [super dealloc];

}

?

@end

?

?

?

?

?

?

?

?

總結(jié)

以上是生活随笔為你收集整理的4.4OC10-内存管理2-set方法的内存管理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。