内存管理相关代码

main.m          

#import <Foundation/Foundation.h>

#import "Student.h"

 

void test() {

    

    Student *stu = [[Student alloc] init];

    

    NSLog(@"count:%zi", [stu retainCount]);   //打印当前计数器值。

    

    [stu retain];

    

    NSLog(@"count:%zi", [stu retainCount]);

    

    [stu release];

    

    NSLog(@"count:%zi", [stu retainCount]);

    

    [stu release];

 

}

 

void test1(Student *stu){

    

    Book *book = [[Book alloc] iniWithPrice:3.5];

    

    stu.book = book;

    

    [book release];

    

    Book *book2 = [[Book alloc] iniWithPrice:4.5];

    

    stu.book = book2;

    

    [book2 release];

    

    

}

 

void test2(Student *stu) {

    [stu readBook];

}

 

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

    @autoreleasepool {

        /*

        Student *stu = [[Student alloc] initWithAge:10];

        

        Book *book = [[Book alloc] iniWithPrice:6.5];

        

        stu.book = book;

        

        NSLog(@"%f",stu.book.price);

        

        [stu release];

        [book release];

        */

        

        Student *stu = [[Student alloc] initWithAge:10];

        

        test1(stu);

        

        test2(stu);

        

        [stu release];

    }

    return 0;

}

 

 

Student.h

 

#import <Foundation/Foundation.h>

#import "Book.h"

 

@interface Student : NSObject {

    int _age;

    Book *_book;

}

 

@property int age;

 

@property Book *book;

 

- (id)initWithAge:(int)age;

 

- (void) readBook;

@end

 

Student.m

#import "Student.h"

 

@implementation Student

 

//@synthesize age = _age;     //在xcode4.5环境下可以省略。

 

#pragma mark - 生命周期方法。

#pragma mark 构造方法

 

- (id)initWithAge:(int)age {

    

    if(self = [super init]) {

        _age = age;

    }

    

    return self;

}

 

#pragma mark 回收对象

 

- (void)dealloc {

    

    

    [_book release];

    

    NSLog(@"student:%i 被销毁了",_age);

    

   [super dealloc]; //一定要调用super的dealloc方法,而且最好放在最后调用。

}

 

#pragma mark - 私有方法

 

#pragma mark getter和setter方法

 

- (void)setAge:(int)age {

    _age = age;

}

 

- (int)age {

    return _age;

}

 

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

    

    if(_book != book){

    //释放之前的成员变量,再retain新传进来的对象。

    

       [_book release];

    

       _book = [book retain];

   }

}

 

- (Book *)book {

    return _book;

}

 

#pragma mark - 公共方法

 

- (void)readBook {

    

    NSLog(@"当前读的书是:%f",_book.price);

    

}

 

@end

 

Book.h

#import <Foundation/Foundation.h>

 

@interface Book : NSObject

 

@property float price;

 

- (id)iniWithPrice:(float)price;

 

@end

 

Book.m

#import "Book.h"

 

@implementation Book

 

#pragma mark - 生命周期方法

 

- (id)iniWithPrice:(float)price {

    if(self = [super init]) {

        _price = price;

        

    }

    

    return self;

}

 

- (void)dealloc {

    

    NSLog(@"Book:%f 被销毁了",_price);

    

    [super dealloc];

}

 

 

 

@end

posted @ 2014-12-19 15:57  unity_lan  阅读(60)  评论(0)    收藏  举报