内存管理2对象之间的内存管理

来源:互联网 发布:淘宝宠物疫苗曝光 编辑:程序博客网 时间:2024/04/29 00:37

Book.h:

#import <Foundation/Foundation.h>@interface Book : NSObject@property float price;-(id)initeWithPrice:(float)price;@end

Book.m:

#import "Book.h"@implementation Book#pragma mark 构造方法-(id)initeWithPrice:(float)price{    if(self=[super init])    {        _price=price;    }    return self;}#pragma mark 回收方法-(void)dealloc{    NSLog(@"Book:%f 被销毁了",_price);    [super dealloc];}@end

Student.h:

#import <Cocoa/Cocoa.h>@class Book;@interface Student : NSObject{    Book * _book;}@property int age;-(id)initWithAge:(int)age;@property Book * book;-(void)readBook;@end

Student.m:

#import "Student.h"#import "Book.h"@implementation Student#pragma mark - 生命周期方法-(id)initWithAge:(int)age{    if(self=[super init])    {        _age=age;     }    return self;}-(void)dealloc{    [_book release];    NSLog(@"Student: %i被销毁了",_age);    [super dealloc];}//如果手动设置getter和setter方法 Xcode就不会帮我们自动生成synthesize,也就不会自动生成带下划线的属性#pragma mark - 设置getter和setter方法#pragma mark setter方法-(void)setBook:(Book *)book{    if(_book!=book)    {    //先释放旧对象,再retain新对象    [_book release];//OC里面没有空指针释放错误 [nil release]不报错    //book:1    _book=[book retain];    }}#pragma mark getter方法-(Book *)book{    return _book;}#pragma mark - 公共方法#pragma mark 读书-(void)readBook{    NSLog(@"当前读的书的价格是:%f",_book.price);}#pragma mark - 私有方法#pragma mark 私有方法1-(void)test1{    }#pragma mark 私有方法2-(void)test2{}#pragma mark 私有方法3-(void)test3{}@end

main:

#import <Foundation/Foundation.h>#import "Student.h"#import "Book.h"void test(Student * stu){    //book:1    Book * book=[[Book alloc] initeWithPrice:11.2];    //book:2    stu.book=book;    //book:1    [book release];    Book * book1=[[Book alloc] initeWithPrice:22.2];    stu.book=book1;    [book1 release];}void test1(Student *stu){    [stu readBook];}int main(int argc, const char * argv[]){    @autoreleasepool {//        Student *stu=[[[Student alloc] initWithAge:10] autorelease];//        NSLog(@"Student age is %i",stu.age);//        Book *book=[[Book alloc] initeWithPrice:11.1f];//        NSLog(@"Book price is %.1f",book.price);////        stu.book=book;//        NSLog(@"%@",stu.book);//        [book release];        //计数器        //stu 1                Student *stu=[[Student alloc] initWithAge:10];        //book:1        //stu:1        test(stu);        //book:1        //stu:1        test1(stu);        //stu:0        //book:0        [stu release];  //在释放stu时候也释放book,因为stu在setter的时候retain了一下,就由它释放        Student * stu1=[[[Student new]initWithAge:20] autorelease];        test(stu1);    }    return 0;}
结果:

2013-08-02 15:01:11.571 内存管理2 a对象之间的内存管理[832:303] Book:11.200000被销毁了

2013-08-02 15:01:11.573 内存管理2 a对象之间的内存管理[832:303]当前读的书的价格是:22.200001

2013-08-02 15:01:11.573 内存管理2 a对象之间的内存管理[832:303] Book:22.200001被销毁了

2013-08-02 15:01:11.574 内存管理2 a对象之间的内存管理[832:303] Student: 10被销毁了

2013-08-02 15:01:11.574 内存管理2 a对象之间的内存管理[832:303] Book:11.200000被销毁了

2013-08-02 15:01:11.574 内存管理2 a对象之间的内存管理[832:303] Book:22.200001被销毁了

2013-08-02 15:01:11.575 内存管理2 a对象之间的内存管理[832:303] Student: 20被销毁了


原创粉丝点击