
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, [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所示的结果。如果出现了什么意外导致错误的话,那么请仔细检查一下你的代码。
总结以上是内存溢出为你收集整理的Objective-C 2.0 with Cocoa Foundation--- 8,类方法以及私有方法(2)全部内容,希望文章能够帮你解决Objective-C 2.0 with Cocoa Foundation--- 8,类方法以及私有方法(2)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)