网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

OC内存管理Demo-创新互联

OC内存管理Demo

创新互联是一家专注于网站建设、网站设计与策划设计,碾子山网站建设哪家好?创新互联做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:碾子山等地区。碾子山做网站价格咨询:18982081108

//主函数

#import

#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对象使用完毕,释放内存,此时benz、engine、lamp引用计数全部为0,系统自动调用dealloc方法销毁内存

  //RunLoop10秒后停止

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

  return 0;

}

Car类:

//car.h

#import

#import "Engine.h"

#import "Lamp.h"

@interface Car : NSObject

{

  NSString *_name;    //名字

  NSString *_licence;   //车牌号

  Engine *_engine;    //引擎对象

  Lamp *_lamp;      //车灯对象

}

//对4个实例变量使用@property生成set和get方法

@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

@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

@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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前标题:OC内存管理Demo-创新互联
本文URL:http://bjjierui.cn/article/jpdcp.html

其他资讯