首页 > 代码库 > OC实例之汽车启动问题

OC实例之汽车启动问题

1.Car.h文件

#import <Foundation/Foundation.h>

//如果A类.h引入B类的.h文件,B类.h文件又引入A类的头文件,就会导致头文件循环引入的错误

//解决方案:

//以后只有两种情况需要在.h里面引其他的头文件, 1是继承, 2是协议

//除此之外需要引头文件,全部到.m文件中引入     .h中如果要使用该类型,使用@class关键字,@class的作用是告诉编译器他是一个类型,并不会把该文件中的内容引入过来

@class Engine,Tire;                   //@class

@interface Car : NSObject

{           //定义汽车类实例变量

    NSString * _brand;

    NSString * _color;

    CGFloat    _price;

    Engine   * _engine;//引擎

    Tire     * _tire;//轮胎

}

汽车设置器与访问器

- (void)setBrand:(NSString *)brand;

- (NSString *)brand;

- (void)setCplor:(NSString *)color;

- (NSString *)color;

- (void)setPrice:(CGFloat)price;

- (CGFloat)price;

//自定义初始化方法

- (id)initwithBrand:(NSString *)brand;

- (id)initwithColor:(NSString *)color;

- (id)initwithPrice:(CGFloat)price;

- (id)initwithBrand:(NSString *)brand

              color:(NSString *)color;

- (id)initwithBrand:(NSString *)brand

              price:(CGFloat)price;

- (id)initwithColor:(NSString *)color

              price:(CGFloat)price;

- (id)initwithBrand:(NSString *)brand

              color:(NSString *)color

              price:(CGFloat)price;

//汽车启动方法

- (void)start;


- (void)setEngine:(Engine *)engine;//汽车发动机设置器与访问器

- (Engine *)engine;


- (void)setTire:(Tire *)tire;//轮胎设置器与访问器

- (Tire *)tire;

@end

2.Car.m文件

#import "Car.h"

#import "Engine.h"

#import "Tire.h"       //注意此处要引入头文件

@implementation Car

- (void)setBrand:(NSString *)brand

{

    _brand = brand;

}

- (NSString *)brand

{

    return _brand;

}

- (void)setCplor:(NSString *)color

{

    _color = color;

}

- (NSString *)color

{

    return _color;

}

- (void)setPrice:(CGFloat)price

{

    _price = price;

}

- (CGFloat)price

{

    return _price;

}


- (id)initwithBrand:(NSString *)brand

{

    _brand = brand;

    return self;

}

- (id)initwithColor:(NSString *)color

{

    _color = color;

    return self;

}

- (id)initwithPrice:(CGFloat)price

{

    _price = price;

    return self;

}

- (id)initwithBrand:(NSString *)brand

              color:(NSString *)color

{

    _brand = brand;

    _color = color;

    return self;

}

- (id)initwithBrand:(NSString *)brand

              price:(CGFloat)price

{

    _brand = brand;

    _price = price;

    return self;

}


- (id)initwithColor:(NSString *)color

              price:(CGFloat)price

{

    _color = color;

    _price = price;

    return self;

}


- (id)initwithBrand:(NSString *)brand

              color:(NSString *)color

              price:(CGFloat)price

{

    _brand = brand;

    _color = color;

    _price = price;

    return self;

}

- (void)setEngine:(Engine *)engine

{

    _engine = engine;

}

- (Engine *)engine

{

    return _engine;

}

- (void)setTire:(Tire *)tire

{

    _tire = tire;

}

- (Tire *)tire

{

    return _tire;

}

- (void)start

{

    BOOL result = [_engine fire];    //调用发动机点火行为

    //点火成功则轮胎转动

    if (result) {                //result = 1则调用轮胎turn(转动)行为

        [_tire turn];

    }

    else{

        NSLog(@"请重试.....!");

    }

}

@end

3.Tire.h

#import <Foundation/Foundation.h>




@interface Tire : NSObject

{

    NSString * _brand;//品牌

    CGFloat    _size;//尺寸

}

- (void)turn;//转动


- (void)setBrand:(NSString *)brand;

- (NSString *)brand;


- (void)setSize:(CGFloat)size;

- (CGFloat)size;


- (id)initwithBrand:(NSString *)brand

               size:(CGFloat)size;


@end

4.Tire.m

#import "Tire.h"


@implementation Tire

- (void)setBrand:(NSString *)brand

{

    _brand = brand;

}

- (NSString *)brand

{

    return _brand;

}


- (void)setSize:(CGFloat)size

{

    _size = size;

}

- (CGFloat)size

{

    return _size;

}


- (id)initwithBrand:(NSString *)brand

               size:(CGFloat)size

{

    _brand = brand;

    _size = size;

    return self;

}


- (void)turn//转动

{

    NSLog(@"轮胎转动");

}

@end

5.Engine.h

#import <Foundation/Foundation.h>


@interface Engine : NSObject

{

    NSString * _brand;//品牌

    CGFloat    _power;//功率

    CGFloat    _displacement;//排量

}

- (BOOL)fire;


- (void)setBrand:(NSString *)brand;

- (NSString *)brand;


- (void)setPower:(CGFloat)power;

- (CGFloat)power;


- (void)setDisplacement:(CGFloat)displacement;

- (CGFloat)displacement;


- (id)initwithBrand:(NSString *)brand

              power:(CGFloat)power

       displacement:(CGFloat)displacement;


@end

6.Engine

#import "Engine.h"

@implementation Engine

- (BOOL)fire            //返回的是一个BOOL值

{

    unsigned long n = arc4random() % 11;     //发动机行为的实现

    if (n % 3 != 0) {

        NSLog(@"点火成功");

        return YES;

    }

    NSLog(@"点火失败");

    return NO;

}


- (void)setBrand:(NSString *)brand

{

    _brand = brand;

}

- (NSString *)brand

{

    return _brand;

}


- (void)setPower:(CGFloat)power

{

    _power = power;

}

- (CGFloat)power

{

    return _power;

}


- (void)setDisplacement:(CGFloat)displacement

{

    _displacement = displacement;

}

- (CGFloat)displacement

{

    return _displacement;

}

- (id)initwithBrand:(NSString *)brand

              power:(CGFloat)power

       displacement:(CGFloat)displacement

{

    _brand = brand;

    _power = power;

    _displacement = displacement;

    return self;

}


@end

7.main.m

  //创建一个引擎

    Engine * engine = [[Engine alloc]initwithBrand:@"东风日产" power:130 displacement:1.8];

    

    //创建一个轮胎

    Tire * tire = [[Tire alloc]initwithBrand:@"米其林" size:100];

    

    //创建一个车对象

    Car * car = [[Car alloc]initwithBrand:@"奥迪A4" color:@"白色" price:300000];

    //给汽车引擎类型的实例变量赋值(给车装上发动机)

    [car setEngine:engine];

    //装上轮胎

    [car setTire:tire];

    

    [car start];



本文出自 “开创未来” 博客,请务必保留此出处http://zhaopeichina.blog.51cto.com/9782414/1602650

OC实例之汽车启动问题