首页 > 代码库 > 类与类之间的简单传值

类与类之间的简单传值

Main.m

#import <Foundation/Foundation.h>
#import "Car.h"
#import "Engine.h"
#import "Lamp.h"

/*
 设计如下几个类,Car自定义初始化方法,初始化方法传入引擎对象和车灯对象。
    当车启动的时候,会调用引擎转动,车灯亮灯,当车停止的时候调用引擎停止转动,车灯熄灭。
 */

int main(int argc, const char * argv[])
{

    //创建引擎对象
    Engine *engine = [[Engine alloc] initWithModel:@"1234567" withCapacity:3.14];
    //创建车灯对象
    Lamp *lamp = [[Lamp alloc] initWithWattage:60.0];
    
    //创建car对象
    Car *car = [[Car alloc] initWithEngine:engine withLamp:lamp];
    [car setName:@"兰博基尼" withLicence:@"8888888"];
    
    int s;
    while (1) {
        NSLog(@"请输入指令(1:启动   2:停止):");
        scanf("%d",&s);
        
        if (s == 1) {
            [car run];
        }else if (s == 2) {
            [car stop];
        }else {
            NSLog(@"输入非法");
        }
    }
    
    return 0;
}

Car.h

#import <Foundation/Foundation.h>
#import "Engine.h"
#import "Lamp.h"

@interface Car : NSObject {

    NSString *_name;    //车名
    NSString *_licence; //车牌号
    
    Engine *_engine;    //引擎
    Lamp *_lamp;    //车灯
    
}

//自定义初始化方法
- (id)initWithEngine:(Engine *)engine withLamp:(Lamp *)lamp;

- (void)setName:(NSString *)name withLicence:(NSString *)licence;

//车启动
- (void)run;
//车停止
- (void)stop;

Car.m

#import "Car.h"

@implementation Car

//自定义初始化方法
- (id)initWithEngine:(Engine *)engine withLamp:(Lamp *)lamp {

    self = [super init];
    
    if (self != nil) {
        _engine = engine;
        _lamp = lamp;
    }
    return self;
}

- (void)setName:(NSString *)name withLicence:(NSString *)licence {

    _name = name;
    _licence = licence;
    
}

//车启动
- (void)run {

    NSLog(@"车牌号为:%@的%@车启动了",_licence,_name);
    
    [_engine turn];
    [_lamp light];
    
}

//车停止
- (void)stop {
    NSLog(@"车牌号为:%@的%@车停止了",_licence,_name);
    
    [_engine stopTurn];
    [_lamp dark];
}
Engine.h

#import <Foundation/Foundation.h>

@interface Engine : NSObject{
    
    NSString *_model;   //型号
    float _capacity;    //排量
    
}

//自定义初始化方法
- (id)initWithModel:(NSString *)model withCapacity:(float)capacity;

//转动
- (void)turn;
//停止转动
- (void)stopTurn;

Engine.m

//自定义初始化方法
- (id)initWithModel:(NSString *)model withCapacity:(float)capacity {

    self = [super init];
    
    if (self != nil) {
        _model = model;
        _capacity = capacity;
    }
    
    return self;
}

//转动
- (void)turn {

    NSLog(@"型号为:%@ 排量为:%.2f的引擎转动了",_model,_capacity);
    
}

//停止转动
- (void)stopTurn {

    NSLog(@"型号为:%@ 排量为:%.2f的引擎停止转动了",_model,_capacity);
    
}

Lamp.h

@interface Lamp : NSObject {

    float _wattage; //瓦特
    
}

//自定义初始化的方法
- (id)initWithWattage:(float)wattage;

//灯亮
- (void)light;
//熄灯
- (void)dark;

Lamp.m

//自定义初始化的方法
- (id)initWithWattage:(float)wattage {

    self = [super init];
    
    if (self != nil) {
        _wattage = wattage;
    }
    
    return self;
}

//灯亮
- (void)light {

    NSLog(@"瓦特数:%.2f的灯亮了",_wattage);
    
}

//熄灯
- (void)dark {

    NSLog(@"瓦特数:%.2f的灯灭了",_wattage);
    
}



类与类之间的简单传值