温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

OC内存管理Demo

发布时间:2020-06-13 07:18:05 阅读:330 作者:GreyGoo 栏目:开发技术
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

OC内存管理Demo

//主函数

#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后,引用计数-1,还剩1

    

    benz.lamp = lamp;

    [lamp release];             //lamp对象赋给benz后,引用计数-1,还剩1

    

    [benz run];                                 //调用run方法

    

    //为上述方法设置定时器

//    [NSTimer scheduledTimerWithTimeInterval:1

//                                     target:benz

//                                   selector:@selector(run)

//                                   userInfo:nil

//                                    repeats:YES];

    

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

    

    [benz stop];                                //调用stop方法

    

    //为上述方法设置定时器

//    [NSTimer scheduledTimerWithTimeInterval:1

//                                     target:benz

//                                   selector:@selector(stop)

//                                   userInfo:nil

//                                    repeats:YES];

    

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

    

    

    [benz release];//benz对象使用完毕,释放内存,此时benzenginelamp引用计数全部为0,系统自动调用dealloc方法销毁内存

    

    //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个实例变量使用@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;

//启动 方法

- (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(@"车牌号为:%@%@ 启动了", _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;

//转动 方法

- (void)turn;

//停止转动 方法

- (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的引擎 转动了", _model, _capacity);

}

- (void)stopTurn

{

    NSLog(@"型号为%@,排量为%ld的引擎 停止转动了", _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(@"瓦数为%d的灯亮了", _wattage);

}

//灯灭 方法

- (void)dark

{

    NSLog(@"瓦数为%d的灯灭了", _wattage);

}

- (void)dealloc

{

    NSLog(@"瓦数为%d的灯 卒!", _wattage);

    [super dealloc];

}

@end

OC内存管理Demo

OC内存管理Demo

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×