Objective-C对象初始化和新特性

一。对象初始化

两种创建对象的方法:a.[类名 new];b.[[类名 alloc] init];

这两种方法是等价的,不过通常的Cocoa惯例是使用b方法,而不是用a方法。

1.编写初始化方法

- (id) init{
	/*实例变量所在的内存位置到隐藏的self参数之间的距离是固定的,
	*如果从init方法返回一个新对象,则需要更新self,以便其后的任何实例变量的引用可以被映射到正确的内存位置
	*初始化时init返回值为nil,表明未能初始化对象,那么if方法体内的代码不会执行,
	*像这样将赋值和检查非零值合并起来是一种典型的C风格的方法。
	*/
	if(self = [super init]){
		engine = [Engine new];
		tires[0] = [Tire new]	
	}
	return self;
 }//init

2.清理Car类

//使用NSMutableArray来代替Car类中常规的C数组
#import <Cocoa/Cocoa.h>

@class Tire;
@class Engine;
@interface Car:Object{
	NSMultableArray *tires;
	Engine *engine;
}

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

- (void) setTire: (Tire *) tire
	atIndex: (int) index;
- (Tire *) tireAtIndex: (int) index;
- (void) print;
@end //Car
 

同时修改Car类的每一个方法

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

创建NSMultableArray,

a.用replaceObjectAtIndex:withObject:代替setTire:atIndex:

(原理是NSMutableArray数组不包含任何内容,使用了NSNull类的对象作为占位符,一般情况下不需要使用NSNull类对象预置NSMutalbeArray数组)

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

b.用数组提供的objectAtIndex:方法代替getter方法tireAtIndex:从中获取tire对象

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

最后仍要确保对象的清理

- (void) dealloc{
	[tire release];
	[engine release];
	[super dealloc];
}//dealloc

3.构造便利初始化函数

@interface Tire:NSObject
{
	float pressure;
	float treadDepth;
}
- (id) initWithPressure: (float) pressure
		treadDepth: (float) treadDepth;

- (void) setPressure: (float) pressure;
- (float) pressure;

- (void) setTreadDepth: (float) treadDepth;
- (fload) treadDepth;
@end //Tire
 

初始化函数更易阅读,减少代码量

  - (id) initWithPressure: (float) p

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

可做如下调用

Tire *tire;
tire = [[Tire alloc] initWithPressure: 23
			treadDepth:33];
 

4.当有多个初始化函数时,对子类进行实例化容易发生调用错误,所以需要指定初始化函数,然后在其他的初始化函数中调用指定的初始化函数。

- (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
 

二 特性

此Objective-C2.0特性只适用于Mac OS X 10.5(Leopard)或更高版本。

1.简化接口

#import <Foundation/Foundation.h>
#import "Tire.h"
@interface Tire:NSObject
{
	float pressure;
	float treadDepth;
}
- (void) setPressure: (float) pressure;
- (float) pressure;

- (void) setTreadDepth: (float) treadDepth;
- (fload) treadDepth;
@end //Tire
 

简化如下#import <Foundation/Foundation.h>

#import "Tire.h"
@interface Tire:NSObject
{
	float pressure;
	float treadDepth;
}
@property float pressure;
@property float treadDepth;
@end // Tire;
 

@property预编译指令的作用是自动声明属性的setter和getter方法。

2.简化实现

@impletation Tire
- (void) setPressure: (float) p{
	pressure = p;
}//setPressure
- (void) pressure{
	return (pressure);
}//setPressure
@end //Tire
简化如下
@impletation Tire
@synthesize pressure;
@end //Tire

@synthesize也是一种新的编译器功能,表示"创建该属性的访问器"。

3.特性扩展

a.读写特性(生成setter和getter方法)

@property (readwrite,copy) NSString *name;
@property (readwrite,retain) Engine *engine;

b.只读特性(getter方法)

@property (readonly) float size;

相关推荐