4.4OC10-内存管理2-set步骤的内存管理
4.4OC10-内存管理2-set方法的内存管理
例一:
main.m
//
// main.m // OC10-内存管理2-set方法的内存管理 // // 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-内存管理2-set方法的内存管理 // // 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 // OC10-内存管理2-set方法的内存管理 // // Created by liuyes on 13-12-9. // Copyright (c) 2013年 renhe. All rights reserved. //
#import "Student.h"
@implementation Student
#pragma mark 构造方法 - (id)initWithAge:(int)age{ if( self = [super init]){ _age = age; } returnself; }
#pragma mark 回收对象 - (void)dealloc{ NSLog(@"student:%i 被销毁了", _age); [super release]; }
@end |
//
// Book.h // OC10-内存管理2-set方法的内存管理 // // Created by liuyes on 13-12-9. // Copyright (c) 2013年 renhe. All rights reserved. //
#import <Foundation/Foundation.h>
@interface Book : NSObject @propertyfloat price; //价格
- (id)initWithPrice:(float)price;
@end |
//
// Book.m // OC10-内存管理2-set方法的内存管理 // // 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 |
运行结果
2013-12-09 17:04:29.679 OC10-内存管理2-set方法的内存管理[634:403] book:3.500000 被销毁了 2013-12-09 17:04:29.703 OC10-内存管理2-set方法的内存管理[634:403] student:29 被销毁了 |
//
// main.m
// OC10-内存管理2-set方法的内存管理
//
// 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-内存管理2-set方法的内存管理
//
// 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-内存管理2-set方法的内存管理
//
// Created by liuyes on 13-12-9.
// Copyright (c) 2013年 renhe. All rights reserved.
//
#import "Student.h"
@implementation Student
#pragma mark 构造方法
- (id)initWithAge:(int)age{
if( self = [super init]){
_age = age;
}
returnself;
}
#pragma mark 回收对象
- (void)dealloc{
//释放对象
[_book release];
//[self.book release];
NSLog(@"student:%i 被销毁了", _age);
[super dealloc];
}
#pragma mark - getter和setter方法
//@synthesize book = _book;
//如果自己手动实现了getter和setter,xcode就不会自动生成@synthesize
//也就不会自动生成_book
//getter和setter的默认实现
- (void)setBook:(Book *)book{
if(_book != book){
//先释放旧的成员变量
[_book release];
//在retain新传进来的对象
_book = [book retain];
}
}
- (Book *)book{
return _book;
}
#pragma mark - 公共方法
#pragma mark 读书
- (void)readBook{
NSLog(@"当前读的书是:%f", _book.price);
}
@end
Book.h
//
// Book.h
// OC10-内存管理2-set方法的内存管理
//
// Created by liuyes on 13-12-9.
// Copyright (c) 2013年 renhe. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Book : NSObject
@propertyfloat price; //价格
- (id)initWithPrice:(float)price;
@end
Book.m
//
// Book.m
// OC10-内存管理2-set方法的内存管理
//
// 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