CHAPTER3 Practice (Object-C)

//
//  main.m
//  CAR3
//
//  Created by jimzhai on 13-1-24.
//  Copyright (c) 2013年 jimzhai. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Car.h"
int main(int argc, const char * argv[])
{

    Car *car = [Car new];
    [car print];
    Engine *engine2 = [Engine new];
    [engine2 setBrand:@"兰博基尼1026"];
    [car setEngine:engine2];
    
    Tire *newTire = [Tire new];
    [newTire setBrand:@"永久"];
    [car setTire:newTire atIndex:2];
    [car print];
    
    return 0;
}
//
//  Car.h
//  CAR3
//
//  Created by jimzhai on 13-1-24.
//  Copyright (c) 2013年 jimzhai. All rights reserved.
//

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

@interface Car : NSObject
{
    Engine *engine;
    Tire *tire[4];
}

- (void) print;

- (void) setTire: (Tire*)tire atIndex: (int)index;

- (Tire*)tireAtIndex: (int)index;

- (void)setEngine: (Engine*)engine;

- (Engine*)getEngine;

@end//Car
//
//  Car.m
//  CAR3
//
//  Created by jimzhai on 13-1-24.
//  Copyright (c) 2013年 jimzhai. All rights reserved.
//

#import "Car.h"

@implementation Car
- (id) init
{
    if(self = [super init])
    {
        engine = [Engine new];
        [engine setBrand:@"法拉利10016"];
        
        tire[0] = [Tire new];
        tire[1] = [Tire new];
        tire[2] = [Tire new];
        tire[3] = [Tire new];
        
        for(int i = 0 ; i < 4 ; i++)
            [tire[i] setBrand:@"米其林"];
    }
    return self;
}//init

- (void) print
{
    NSLog(@"%@" , engine);
    
    NSLog(@"%@" , tire[0]);
    NSLog(@"%@" , tire[1]);
    NSLog(@"%@" , tire[2]);
    NSLog(@"%@" , tire[3]);
}// print

- (void) setTire:(Tire *)_tire atIndex: (int) index
{
    if(index < 0 || index > 3)
    {
        NSLog(@"Index is wrong!");
        exit(1);
    }
    tire[index] = _tire;
}//setTire

- (Tire*) tireAtIndex:(int)index
{
    if(index<0||index>3)
    {
        NSLog(@"index is wrong");
        exit(1);
    }
    return tire[index];
}//tireAtIndex

- (void)setEngine:(Engine *)_engine
{
    engine = _engine;
}//setEngine

- (Engine*)getEngine
{
    return engine;
}//getEngine

@end//Car

//
//  Engine.h
//  CAR3
//
//  Created by jimzhai on 13-1-24.
//  Copyright (c) 2013年 jimzhai. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Engine : NSObject
{
    NSString *brand;
}

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

- (NSString*) getBrand;

@end

//
//  Engine.m
//  CAR3
//
//  Created by jimzhai on 13-1-24.
//  Copyright (c) 2013年 jimzhai. All rights reserved.
//

#import "Engine.h"

@implementation Engine

- (NSString*) description
{
    return [NSString stringWithFormat:@"I am engine ! my brand is %@",brand];
}//description

- (void) setBrand:(NSString *)_brand
{
    brand = _brand;
}//setBrand

- (NSString*) getBrand
{
    return brand;
}//getBrand

@end//Engine

//
//  Tire.h
//  CAR3
//
//  Created by jimzhai on 13-1-24.
//  Copyright (c) 2013年 jimzhai. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Tire : NSObject
{
    NSString *brand;
}

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

- (NSString*) getBrand;

@end//Tire

//
//  Tire.m
//  CAR3
//
//  Created by jimzhai on 13-1-24.
//  Copyright (c) 2013年 jimzhai. All rights reserved.
//

#import "Tire.h"

@implementation Tire

- (NSString*) description
{
    return [NSString stringWithFormat:@"I am a tire. My brand is %@",brand];
}//description

- (void) setBrand:(NSString *)brandl
{
    brand = brandl ;
}//setBrand

-(NSString*) getBrand
{
    return brand;
}//getBrand

@end//Tire








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值