oc_study19--多对象内存管理

来源:互联网 发布:淘宝网回力鞋 编辑:程序博客网 时间:2024/06/03 22:06

main.m

//内容:Book与Person对象内存管理#import <Foundation/Foundation.h>#import "Book.h"#import "Person.h"int main(void){    Book * b = [[Book alloc] init];    Person * p1 = [[Person alloc] init];        //p1占用书b    [p1 setBook:b];    [b release];    b = nil;            [p1 release];    p1 = nil;    return 0;}

Book.h

#import <Foundation/Foundation.h>@interface Book : NSObject{    int _price;}- (void)setPrice:(int)price;- (int)price;@end

Book.m

#import "Book.h"@implementation Book- (void)setPrice:(int)price{    _price = price;}- (int)price{    return _price;}- (void)dealloc{    NSLog(@"Book对象被回收");    [super dealloc];}@end

Person.h

#import "Person.h"@implementation Person- (void)setBook:(Book *)book{    _book = [book retain];}- (Book *)book{    return _book;}- (void)dealloc{    [_book release];    NSLog(@"Person对象被回收");    [super dealloc];}@end

Person.m

#import "Person.h"@implementation Person- (void)setBook:(Book *)book{    _book = [book retain];}- (Book *)book{    return _book;}- (void)dealloc{    [_book release];    NSLog(@"Person对象被回收");    [super dealloc];}@end



0 0