Object - C 학습 코드 [제1 1 장, 속성 학습]

//
//  main.m
//  PropertyCar
//
//  Created by on 14-9-9.
//  Copyright (c) 2014  apple. All rights reserved.
//

//    ,    
#import <Foundation/Foundation.h>
#import "Car.h"
#import "Slant6.h"
#import "AllWeatherRadial.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        Car *car = [[Car alloc] init];
        car.name = @"Herbie";
        
        for (int i = 0; i < 4; i++)
        {
            AllWeatherRadial *tire;
            
            tire = [[AllWeatherRadial alloc] init];
            tire.rainHandling = 20+i;
            tire.snowHandling = 28+i;
            NSLog(@"tire %d's handling is %.f %.f", i, tire.rainHandling, tire.snowHandling);
            
            [car setTire:tire atIndex:i];
        }
        
        car.engine = [[Slant6 alloc] init];;
        
        [car print];
    }
	return (0);
}

//
//  Tire.h
//  newCar
//
//  Created by on 14-8-19.
//  Copyright (c) 2014  com.newCar. All rights reserved.
//

@interface Tire : NSObject

@property float pressure;
@property float treadDepth;

- (id) initWithPressure: (float) pressure;
- (id) initWithTreadDepth: (float) treadDepth;

- (id) initWithPressure: (float) pressure
             treadDepth: (float) treadDepth;
@end // Tire

//
//  Tire.m
//  newCar
//
//  Created by on 14-8-19.
//  Copyright (c) 2014  com.newCar. All rights reserved.
//

#import "Tire.h"

@implementation Tire
@synthesize pressure;
@synthesize treadDepth;

- (id) init
{
    if (self = [self initWithPressure: 34
                           treadDepth: 20]) {
    }
    
    return (self);
    
} // init


- (id) initWithPressure: (float) p
{
    if (self = [self initWithPressure: p
                           treadDepth: 20.0]) {
    }
    
    return (self);
    
} // initWithPressure


- (id) initWithTreadDepth: (float) td
{
    if (self = [self initWithPressure: 34.0
                           treadDepth: td]) {
    }
    
    return (self);
    
} // initWithTreadDepth

- (id) initWithPressure: (float) p
             treadDepth: (float) td
{
    if (self = [super init]) {
        pressure = p;
        treadDepth = td;
    }
    
    return (self);
    
} // initWithPressure:treadDepth:

- (NSString *) description
{
    NSString *desc;
    desc = [NSString stringWithFormat:
            @"Tire: Pressure: %.1f TreadDepth: %.1f",
            pressure, treadDepth];
    return (desc);
    
} // description

@end // Tire

//
//  Engine.h
//  newCar
//
//  Created by on 14-8-19.
//  Copyright (c) 2014  com.newCar. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Engine : NSObject

@end

//
//  Engine.m
//  newCar
//
//  Created by on 14-8-19.
//  Copyright (c) 2014  com.newCar. All rights reserved.
//

#import "Engine.h"

@implementation Engine
- (NSString *)description {
    return (@"I am an engine. Vrooom!");
} // description
@end // Engine

//
//  Slant6.h
//  newCar
//
//  Created by on 14-8-20.
//  Copyright (c) 2014  com.newCar. All rights reserved.
//

#import "Engine.h"

@interface Slant6 : Engine

@end // Slant6

//
//  Slant6.m
//  newCar
//
//  Created by on 14-8-20.
//  Copyright (c) 2014  com.newCar. All rights reserved.
//

#import "Slant6.h"

@implementation Slant6
- (NSString *)description {
    return (@"I am a Slant-6, Vrooom!");
} // description
@end // Slant6

//
//  AllWeatherRadial.h
//  newCar
//
//  Created by on 14-8-20.
//  Copyright (c) 2014  com.newCar. All rights reserved.
//

#import "Tire.h"

@interface AllWeatherRadial : Tire
//             ,        .m      ,    .h           
//                  。     ,           
//{
//    float rainHandling;
//    float snowHandling;
//}
//@property           ,               
//@property                setter getter  。
//    -setRainHandling:      
//    -rainHanding     
@property float rainHandling;
@property float snowHandling;
@end // AllWeatherRadial

//
//  AllWeatherRadial.m
//  newCar
//
//  Created by on 14-8-20.
//  Copyright (c) 2014  com.newCar. All rights reserved.
//

#import "AllWeatherRadial.h"

@implementation AllWeatherRadial
//          ,           ,          
//{
//    float rainHandling;
//    float snowHandling;
//}
//@synthesize          ,   “           ”。
//   @synthesize rainHandling;     ,        -setRainHandling:   -rainHandling        
@synthesize rainHandling;
@synthesize snowHandling;

- (id) initWithPressure:(float)p
             treadDepth:(float)td
{
    if (self = [super initWithPressure: p
                            treadDepth: td]) {
        rainHandling = 24.7;
        snowHandling = 42.5;
    }
    
    return (self);
    
} // initWithPressure:treadDepth

- (NSString *) description
{
    NSString *desc;
    desc = [[NSString alloc] initWithFormat:
            @"AllWeatherRadial: %.1f / %.1f / %.1f / %.1f",
//                     
//                 Object-C        。
//                        (=)  ,      setter      
//                             ,       getter      
            self.pressure, self.treadDepth, self.rainHandling, self.snowHandling];
    
    return (desc);
    
} // description

@end // AllWeatherRadial

//
//  Car.h
//  newCar
//
//  Created by on 14-8-19.
//  Copyright (c) 2014  com.newCar. All rights reserved.
//

@class Tire;
@class Engine;

@interface Car : NSObject

@property (copy) NSString *name;
@property (strong) Engine *engine;

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

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

- (void) print;

@end // Car

//
//  Car.m
//  newCar
//
//  Created by on 14-8-19.
//  Copyright (c) 2014  com.newCar. All rights reserved.
//

#import "Car.h"
#import "Engine.h"

@implementation Car
{
    NSMutableArray *tires;
}

@synthesize name = appellation;
@synthesize engine;

- (id) init
{
    if (self = [super init]) {
        self.name = @"Car";
        
        tires = [[NSMutableArray alloc] init];
        
        for (int i = 0; i < 4; i++)
        {
            [tires addObject: [NSNull null]];
        }
    }
    
    return (self);
    
} // init

// dealloc

- (void) setTire: (Tire *) tire
         atIndex: (int) index
{
    [tires replaceObjectAtIndex: index
                     withObject: tire];
    
} // setTire:atIndex:


- (Tire *) tireAtIndex: (int) index
{
    Tire *tire;
    tire = [tires objectAtIndex: index];
    
    return (tire);
    
} // tireAtIndex:


- (void) print
{
    NSLog (@"%@ has:", self.name);
    
    for (int i = 0; i < 4; i++)
    {
        NSLog (@"%@", [self tireAtIndex: i]);
    }
    
    NSLog (@"%@", engine);
    
} // print

@end // Car

//
//  main.m
//  PropertyCar
//
//  Created by on 14-9-9.
//  Copyright (c) 2014  apple. All rights reserved.
//

//    ,    
#import <Foundation/Foundation.h>
#import "Car.h"
#import "Slant6.h"
#import "AllWeatherRadial.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        Car *car = [[Car alloc] init];
        car.name = @"Herbie";
        
        for (int i = 0; i < 4; i++)
        {
            AllWeatherRadial *tire;
            
            tire = [[AllWeatherRadial alloc] init];
            tire.rainHandling = 20+i;
            tire.snowHandling = 28+i;
            NSLog(@"tire %d's handling is %.f %.f", i, tire.rainHandling, tire.snowHandling);
            
            [car setTire:tire atIndex:i];
        }
        
        car.engine = [[Slant6 alloc] init];;
        
        [car print];
    }
	return (0);
}

좋은 웹페이지 즐겨찾기