您好,登录后才能下订单哦!
//主函数
#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对象使用完毕,释放内存,此时benz、engine、lamp引用计数全部为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生成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 <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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。