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