类与类之间的简单传值

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);
    
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值