main.m
#import <Cocoa/Cocoa.h>
#import "Car.h"
#import "Tires.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Car *car=[[Car alloc]init];
NSLog(@"%@",car);
Tires *t0=[[Tires alloc]init];
Tires *t1=[[Tires alloc]initWithPressure:14.4];
Tires *t2=[[Tires alloc]initWithTresdDegh:25.6];
Tires *t3=[[Tires alloc]initWithPressure:12.1 andTresdDegh:25.6];
[car SetTires:t0 atIndex:LeftFront];
[car SetTires:t1 atIndex:leftBack];
[car SetTires:t2 atIndex:rightFront];
[car SetTires:t3 atIndex:rightBack];
NSLog(@"%@",car);
}
return 0;
}
car.h
#import <Foundation/Foundation.h>
#import "Engine.h"
#import "Tires.h"
typedef enum//定义四个轮子的枚举
{
LeftFront,
leftBack,
rightFront,
rightBack
}TIreIndex;
@interface Car : NSObject
{
Engine *_engine; //定义发动机的变量类型是Engine型的
NSMutableArray *_tires;//定义轮子的可变数组
}
-(void)SetEngine:(Engine*)engine;
-(Engine *)engine;
-(void)SetTires:(Tires*)tires atIndex:(TIreIndex)index;
-(Tires*)tiresindex:(TIreIndex)index;
@end
car.m
#import "Car.h"
@implementation Car
-(id)init
{
if (self=[super init]) {
_engine=[[Engine alloc]init];//用init的形式初始化轮胎和发动机的变量
_tires =[NSMutableArray array];
for (int i=0; i<4; i++) {
[_tires addObject:[NSNull null]];//用四个空对象占位
}
}
return self;
}
-(void)SetEngine:(Engine *)engine
{
if (_engine!=engine) {
[_engine release];//用手动的方式进行内存管理
_engine=[engine retain];
}
}
-(Engine*)engine
{
return _engine;
}
-(void)SetTires:(Tires *)tires atIndex:(TIreIndex)index
{
if (index<LeftFront||index>rightBack) {
NSLog(@"Invalid index:(%d) in %s",index,__func__);//做一个判断如果index小于LeftFront的枚举数1或者大于rightBack的枚举数4则输出错误
return;
}
[_tires replaceObjectAtIndex:index withObject:tires];//使用replaceObjectAtIndex方法用tires对象替换可变数组_tires中处于index位置的对象
}
-(Tires*)tiresindex:(TIreIndex)index
{
if (index<LeftFront&&index>rightBack) {
NSLog(@"Invalid index:(%d) in %s",index,__func__);//做一个判断满足条件之后返回空
return nil;
}
return [_tires objectAtIndex:index];//不满足条件的话返回可变数组中index位置的对象
}
-(NSString*)description//调用NSObject类中的description方法,
{
NSString*desc=[NSString stringWithFormat:@"I am car:have a%@ \n four's tires \n %@ \n %@ \n %@ \n %@",_engine,[self tiresindex:LeftFront],_tires[leftBack],_tires[rightFront],_tires[rightBack]];
return desc;
}
@end
Tires.h
#import <Foundation/Foundation.h>
#define kDefautPressure 13.5
#define kDefautTreadDepth 26.4
@interface Tires : NSObject
{
float _pressure;
float _tresdDegth;
}
-(id)initWithPressure:(float)pressure;
-(id)initWithTresdDegh:(float)tresdDegh;
-(id)initWithPressure:(float)pressure andTresdDegh:(float)tresdDegh;
-(void)setPressure:(float)pressure;
-(float)pressure;
-(void)SetTresdDegth:(float)TresdDegth;
-(float)TresdDegth;
@end
Tires.m
#import "Tires.h"
@implementation Tires
-(void)setPressure:(float)pressure
{
_pressure=pressure;
}
-(float)pressure
{
return _pressure;
}
-(void)SetTresdDegth:(float)TresdDegth
{
_tresdDegth=TresdDegth;
}
-(float)TresdDegth
{
return _tresdDegth;
}
#pragma mark-初始化方法
//遍历初始化中调用指定初始化方法 如果用户传进来对应的参数 那么我们就用这个参数 没有传参的 使用默认的参数
-(id)init
{
return [self initWithPressure:kDefautPressure andTresdDegh:kDefautTreadDepth];
}
-(id)initWithPressure:(float)pressure
{
return [self initWithPressure:pressure andTresdDegh:kDefautTreadDepth];
}
-(id)initWithTresdDegh:(float)tresdDegh
{
return [self initWithPressure:kDefautPressure andTresdDegh:tresdDegh];
}
-(id)initWithPressure:(float)pressure andTresdDegh:(float)tresdDegh
{
if (self=[super init]) {
_pressure=pressure;
_tresdDegth=tresdDegh;
}
return self;
}
-(NSString*)description
{
NSString *desc=[NSString stringWithFormat:@"胎压:%.1f 胎纹深度:%.2f",_pressure,_tresdDegth];
return desc;
}
@end
Engine.h
#import <Foundation/Foundation.h>
@interface Engine : NSObject
@end
Engine.m
#import "Engine.h"
@implementation Engine
-(NSString*)description
{
return @"apple(ios) engine";
}
@end