8.2,实现步骤
第一步,按照我们在第2章所述的方法,新建一个项目,项目的名字叫做07-InitWithAndIvarScope。如果你是第一次看本篇文章,请参看第二章的内容。
第二步,按照我们在第4章的4.2节的第二,三,四步所述的方法,把在第4章已经使用过的“Cattle.h”,“Cattle.m”,“Bull.h”还有“Bull.m”, 导入本章的项目里面。
第三步,打开“Cattle.h”和“Cattle.m”,分别修改成为下面的代码并且保存:
#import
@interface Cattle : NSobjecs {
int legsCount;
}
- (void)saySomething;
+ (id) cattleWithLegsCountVersionA:(int) count;
+ (id) cattleWithLegsCountVersionB:(int) count;
+ (id) cattleWithLegsCountVersionC:(int) count;
+ (id) cattleWithLegsCountVersionD:(int) count;
@end
#import "Cattle.h"
#import
@implementation Cattle
-(void) saySomething
{
NSLog(@ "Hello, I am a cattle, I have %d legs.", legsCount);
}
-(void) setLegsCount:(int) count
{
legsCount = count;
}
+ (id) cattleWithLegsCountVersionA:(int) count
{
id ret = [[Cattle alloc] init];
//NEVER DO LIKE BELOW
//legsCount = count;
[ret setLegsCount:count];
return [ret autorelease];
}
+ (id) cattleWithLegsCountVersionB:(int) count
{
id ret = [[[Cattle alloc] init] autorelease];
[ret setLegsCount:count];
return ret;
}
+ (id) cattleWithLegsCountVersionC:(int) count
{
id ret = [[self alloc] init];
[ret setLegsCount:count];
return [ret autorelease];
}
+ (id) cattleWithLegsCountVersionD:(int) count
{
id ret = [[self alloc] init];
[ret setLegsCount:count];
if([self class] == [Cattle class])
return [ret autorelease];
SEL sayName = @selector(saySomething);
Method unknownSubClassSaySomething = class_getInstanceMethod([self class], sayName);
//Change the subclass method is RUDE!
Method cattleSaySomething = class_getInstanceMethod([Cattle class], sayName);
//method_imp is deprecated since 10.5
unknownSubClassSaySomething- >method_imp = cattleSaySomething->method_imp;
return [ret autorelease];
}
@end
第四步,打开“Bull.h”和“Bull.m”,分别修改成为下面的代码并且保存:
#import
#import "Cattle.h"
@interface Bull : Cattle {
NSString *skinColor;
}
- (void)saySomething;
- (NSString*) getSkinColor;
- (void) setSkinColor:(NSString *) color;
+ (id) bullWithLegsCount:(int) count bullSkinColor:(NSString*) theColor;
@end
#import "Bull.h"
@implementation Bull
-(void) saySomething
{
NSLog(@"Hello, I am a %@ bull, I have %d legs.", [self getSkinColor],legsCount);
}
-(NSString*) getSkinColor
{
return skinColor;
}
- (void) setSkinColor:(NSString *) color
{
skinColor = color;
}
+ (id) bullWithLegsCount:(int) count bullSkinColor:(NSString*) theColor
{
id ret = [self cattleWithLegsCountVersionC:count];
[ret setSkinColor:theColor];
//DO NOT USE autorelease here!
return ret;
}
@end
第五步,创建一个新类,名字叫做“UnknownBull”,然后分别打开“UnknownBull.h”和“UnknownBull.m”,分别修改成为下面的代码并且保存:
#import
#import "Bull.h"
@interface UnknownBull : Bull {
}
-(void)saySomething;
@end
#import "UnknownBull.h"
@implementation UnknownBull
-(void)saySomething
{
NSLog(@ "Hello, I am an unknown bull.");
}
@end
第六步,打开“08-Class_Method_And_Private_Method.m” ,修改成为下面的样子并且保存
#import
#import "Cattle.h"
#import "Bull.h"
#import "UnknownBull.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
id cattle[5];
cattle[0] = [Cattle cattleWithLegsCountVersionA:4];
cattle[1] = [Bull cattleWithLegsCountVersionB:4];
cattle[2] = [Bull cattleWithLegsCountVersionC:4];
cattle[3] = [Bull bullWithLegsCount:4 bullSkinColor:@ "red"];
cattle[4] = [UnknownBull cattleWithLegsCountVersionD:4];
for(int i = 0 ; i < 5 ; i++)
{
[cattle saySomething];
}
[pool drain];
return 0;
}
第七步,选择屏幕上方菜单里面的“Run”,然后选择“Console”,打开了Console对话框之后,选择对话框上部中央的“Build and Go”,如果不出什么意外的话,那么应该出现入图8-1所示的结果。如果出现了什么意外导致错误的话,那么请仔细检查一下你的代码。