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);
}
                이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.