main.m
#import <Cocoa/Cocoa.h>
#import "Car.h"
#import "Tires.h"
int main(int argc,const char * argv[]) {
@autoreleasepool {
Car *car=[[Caralloc]init];
NSLog(@"%@",car);
Tires *t0=[[Tiresalloc]init];
Tires *t1=[[Tiresalloc]initWithPressure:14.4];
Tires *t2=[[Tiresalloc]initWithTresdDegh:25.6];
Tires *t3=[[Tiresalloc]initWithPressure:12.1andTresdDegh:25.6];
[car SetTires:t0atIndex:LeftFront];
[car SetTires:t1atIndex:leftBack];
[car SetTires:t2atIndex:rightFront];
[car SetTires:t3atIndex:rightBack];
NSLog(@"%@",car);
}
return0;
}
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=[superinit]) {
_engine=[[Enginealloc]init];//用init的形式初始化轮胎和发动机的变量
_tires =[NSMutableArrayarray];
for (int i=0; i<4; i++) {
[_tiresaddObject:[NSNullnull]];//用四个空对象占位
}
}
returnself;
}
-(void)SetEngine:(Engine *)engine
{
if (_engine!=engine) {
[_enginerelease];//用手动的方式进行内存管理
_engine=[engineretain];
}
}
-(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;
}
[_tiresreplaceObjectAtIndex:index withObject:tires];//使用replaceObjectAtIndex方法用tires对象替换可变数组_tires中处于index位置的对象
}
-(Tires*)tiresindex:(TIreIndex)index
{
if (index<LeftFront&&index>rightBack) {
NSLog(@"Invalid index:(%d) in %s",index,__func__);//做一个判断满足条件之后返回空
returnnil;
}
return [_tiresobjectAtIndex:index];//不满足条件的话返回可变数组中index位置的对象
}
-(NSString*)description//调用NSObject类中的description方法,
{
NSString*desc=[NSStringstringWithFormat:@"I am car:have a%@ \n four's tires \n %@ \n %@ \n %@ \n %@",_engine,[selftiresindex: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 [selfinitWithPressure:kDefautPressureandTresdDegh:kDefautTreadDepth];
}
-(id)initWithPressure:(float)pressure
{
return [selfinitWithPressure:pressure andTresdDegh:kDefautTreadDepth];
}
-(id)initWithTresdDegh:(float)tresdDegh
{
return [selfinitWithPressure:kDefautPressureandTresdDegh:tresdDegh];
}
-(id)initWithPressure:(float)pressure andTresdDegh:(float)tresdDegh
{
if (self=[superinit]) {
_pressure=pressure;
_tresdDegth=tresdDegh;
}
returnself;
}
-(NSString*)description
{
NSString *desc=[NSStringstringWithFormat:@"胎压:%.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