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| ?// //? 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| ?// //? 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 | 
運行結果
| 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
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的4.4OC10-内存管理2-set方法的内存管理的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 华为od机试题9 真题
- 下一篇: python输入的n打印n行杨辉三角_新
