您好,登錄后才能下訂單哦!
//主函數
#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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。