溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

OC內(nèi)存管理Demo

發(fā)布時(shí)間:2020-06-13 07:18:05 來源:網(wǎng)絡(luò) 閱讀:316 作者:GreyGoo 欄目:開發(fā)技術(shù)

OC內(nèi)存管理Demo


//主函數(shù)

#import <Foundation/Foundation.h>

#import "Car.h"


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

    

    Lamp *lamp = [[Lamp alloc] init];                                     //初始化車燈對象

    lamp.wattage = 75;

    

    Engine *engine = [[Engine alloc] initWithModel:@"MX-8" Capacity:180]; //初始化引擎對象

    

    Car *benz = [[Car alloc] initWithName:@"奔馳" Licence:@"A:DB250"];  //初始化汽車對象

    

    benz.engine = engine;

    [engine release];           //engine對象賦給benz后,引用計(jì)數(shù)-1,還剩1

    

    benz.lamp = lamp;

    [lamp release];             //lamp對象賦給benz后,引用計(jì)數(shù)-1,還剩1

    

    [benz run];                                 //調(diào)用run方法

    

    //為上述方法設(shè)置定時(shí)器

//    [NSTimer scheduledTimerWithTimeInterval:1

//                                     target:benz

//                                   selector:@selector(run)

//                                   userInfo:nil

//                                    repeats:YES];

    

//    NSLog(@"-------------分割線--------------");

    

    [benz stop];                                //調(diào)用stop方法

    

    //為上述方法設(shè)置定時(shí)器

//    [NSTimer scheduledTimerWithTimeInterval:1

//                                     target:benz

//                                   selector:@selector(stop)

//                                   userInfo:nil

//                                    repeats:YES];


    

//    NSLog(@"-------------分割線--------------");

    

    

    [benz release];//benz對象使用完畢,釋放內(nèi)存,此時(shí)benz、engine、lamp引用計(jì)數(shù)全部為0,系統(tǒng)自動(dòng)調(diào)用dealloc方法銷毀內(nèi)存

    

    //RunLoop10秒后停止

//    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]];

    return 0;

}


Car類:


//car.h

#import <Foundation/Foundation.h>

#import "Engine.h"

#import "Lamp.h"


@interface Car : NSObject

{

    NSString *_name;        //名字

    NSString *_licence;     //車牌號

    Engine *_engine;        //引擎對象

    Lamp *_lamp;            //車燈對象

}


//4個(gè)實(shí)例變量使用@property生成setget方法

@property (nonatomic, copy) NSString *name;

@property (nonatomic, copy) NSString *licence;

@property (nonatomic, retain)Engine *engine;

@property (nonatomic, retain)Lamp *lamp;


//自定義初始化方法

- (id)initWithName:(NSString *)name Licence:(NSString *)licence;


//啟動(dòng) 方法

- (void)run;


//停止 方法

- (void)stop;



- (void)dealloc;


@end


//Car.m

#import "Car.h"


@implementation Car


- (id)initWithName:(NSString *)name Licence:(NSString *)licence

{

    self = [super init];

    if (self) {

        _name = name;

        _licence = licence;

    }

    

    return self;

}



- (void)run

{

    NSLog(@"車牌號為:%@%@ 啟動(dòng)了", _licence_name);

    [_lamp light];

    [_engine turn];

    NSLog(@"-------------分割線--------------");


}


- (void)stop

{

    [_lamp dark];

    [_engine stopTurn];

    NSLog(@"車牌號為:%@%@ 停止了", _licence_name);

    NSLog(@"-------------分割線--------------");


}


- (void)dealloc

{

    [_lamp release];

    _lamp = nil;

    [_engine release];

    _engine = nil;

    NSLog(@"車牌號為:%@%@ 卒!", _licence_name);

    [super dealloc];

    self = nil;

}



@end



Engine類:


//Engine.h

#import <Foundation/Foundation.h>


@interface Engine : NSObject

{

    NSString *_model;       //型號

    NSInteger _capacity;    //排量

}


@property (nonatomic, copy) NSString *model;

@property (nonatomic, assign) NSInteger capacity;


//自定義初始化方法

- (id)initWithModel:(NSString *)model Capacity:(NSInteger)capacity;


//轉(zhuǎn)動(dòng) 方法

- (void)turn;


//停止轉(zhuǎn)動(dòng) 方法

- (void)stopTurn;



- (void)dealloc;


@end


//Engine.m

#import "Engine.h"


@implementation Engine


- (id)initWithModel:(NSString *)model Capacity:(NSInteger)capacity

{

    self = [super init];

    if (self) {

        _model = model;

        _capacity = capacity;

    }

    

    return self;

}



- (void)turn

{

    NSLog(@"型號為%@,排量為%ld的引擎 轉(zhuǎn)動(dòng)了", _model, _capacity);

}


- (void)stopTurn

{

    NSLog(@"型號為%@,排量為%ld的引擎 停止轉(zhuǎn)動(dòng)了", _model, _capacity);

}


- (void)dealloc

{

    NSLog(@"型號為%@,排量為%ld的引擎 卒!", _model, _capacity);

    [super dealloc];

}


@end


Lamp類:


//Lamp.h

#import <Foundation/Foundation.h>


@interface Lamp : NSObject

{

    int _wattage;

}


@property (nonatomic, assign) int wattage;


//燈亮 方法

- (void)light;


//燈滅 方法

- (void)dark;


- (void)dealloc;


@end



//Lamp.m

#import "Lamp.h"


@implementation Lamp


//燈亮 方法

- (void)light

{

    NSLog(@"瓦數(shù)為%d的燈亮了", _wattage);

}


//燈滅 方法

- (void)dark

{

    NSLog(@"瓦數(shù)為%d的燈滅了", _wattage);

}


- (void)dealloc

{

    NSLog(@"瓦數(shù)為%d的燈 卒!", _wattage);

    [super dealloc];

}


@end


OC內(nèi)存管理Demo

OC內(nèi)存管理Demo


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI