11.内存管理的set方法

Set方法的代码规范

(1)基本数据类型:直接复制

-voidsetAge:(int)age

{

_age=age;

}

(2)OC对象类型

-voidsetCar:(Car *)car

{

//1.先判断是不是新传进来的对象

If(car!=_car)

{

//2 对旧对象做一次release

[_car release];//若没有旧对象,则没有影响

//3.对新对象做一次retain

_car=[car retain];

}

}

==================================

//  Student.h

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

//



#import <Foundation/Foundation.h>

#import "Book.h"


@interface Student : NSObject {

    Book *_book;

}


@property int age;


- (id)initWithAge:(int)age;


@property Book *book;


- (void)readBook;

@end


============================


//  Student.m

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


#import "Student.h"


@implementation Student


#pragma mark - 生命周期方法

#pragma mark 构造方法

- (id)initWithAge:(int)age {

    if ( self = [super init] ) {

        _age = age;

    }

    

    return self;

}


#pragma mark 回收对象

- (void)dealloc {

    // 释放Book对象

    [_book release];

    

    // [self.book release];

    

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

    [super dealloc];

}


#pragma mark - gettersetter

// @synthesize book = _book;

// 如果自己手动实现了gettersetterxcode就不会自动生成@synthesize

// 也就不会自动生成_book

// gettersetter的默认实现

- (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);

}




//#pragma mark - 私有方法

//#pragma mark 私有方法1

//- (void)test1 {

//

//

//}

//#pragma mark 私有方法2

//- (void)test2 {

//    

//    

//}

//#pragma mark 私有方法3

//- (void)test3 {

//    

//    

//}


@end


=============

//

//  Book.h

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


#import <Foundation/Foundation.h>


@interface Book : NSObject

@property float price; // 价格


- (id)initWithPrice:(float)price;

@end

===========

//  Book.m

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

//


#import "Book.h"


@implementation Book


- (id)initWithPrice:(float)price {

    if ( self = [super init] ) {

        _price = price;

    }

    

    return self;

}


- (void)dealloc {

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

    

    [super dealloc];

}

@end

=============

//  main.m

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


#import <Foundation/Foundation.h>

#import "Book.h"

#import "Student.h"


void test(Student *stu) {

    // book:1

    Book *book = [[Book allocinitWithPrice:3.5];


    // book:2

    stu.book = book;


    // book:1

    [book release];

    

    // book:1

    stu.book = book;

    stu.book = book;

    

     // book2:1

    Book *book2 = [[Book allocinitWithPrice:4.5];

     // book2:2

    stu.book = book2;

    // book2:2

    stu.book = book2;

    // book2:1

    [book2 release];

    

    // book2:1

    stu.book = book2;

}


void test1(Student *stu) {

    [stu readBook];

}


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

{


    @autoreleasepool {

        // stu:1

        Student *stu = [[Student allocinitWithAge:10];

        

        // stu:1

        // book:1

        // book2:1

        test(stu);

        

        // stu:1

        // book:1

        // book2:1

        test1(stu);

        

        // stu:0

        // book2:0

        // book:1

        [stu release];

        

        // stu = nil;  清空stu这个指针,stu就会变成空指针

        

        // [stu release]; // 野指针(会报错)

        

        [nil release]; // 空指针(不会报错)

    }

    return 0;

}




















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值