上一篇展示了如何用傳統的“面向過程編程方法”,實現畫“矩形”、“圓”、“橢圓”,這一篇看下如何改用OOP的方法來實現:
因為要用到“顏色”以及“矩形地區”二個枚舉,先把他們抽出來單獨放在CommDef.h檔案中
//定義“幾何形狀顏色”枚舉typedef enum{kRedColor,kGreenColor,kBlueColor} ShapeColor;//定義“幾何形狀矩形地區”結構typedef struct{int x,y,width,height;} ShapeRect;
然後再定義基類Shape,注意:在obj-C中,定義一個類通常分成二部分,一部分是".h"的檔案,用來申明類有哪些成員(也稱為類的定義檔案,類似於介面),另一部分是".m"的檔案,用來提供具體實現
Shape類的申明部分如下:(Shape.h)
//// Shape.h// HelloWorld//// Created by jimmy.yang on 11-1-26.// Copyright 2011 __MyCompanyName__. All rights reserved.//#import "CommDef.h"@interface Shape : NSObject {ShapeColor fillColor;ShapeRect bounds;}- (void) setFillColor:(ShapeColor) fillColor;- (void) setBounds:(ShapeRect) bounds;- (void) draw;- (NSString*) getColorName:(ShapeColor) fillColor;@end //Shape
Shape類的實現部分如下:(Shape.m)
//// Shape.m// HelloWorld//// Created by jimmy.yang on 11-1-26.// Copyright 2011 __MyCompanyName__. All rights reserved.//#import "Shape.h"@implementation Shape- (void) setBounds:(ShapeRect)b{bounds = b;}- (void) setFillColor:(ShapeColor)f{fillColor =f;}//註:這個方法留給子類去實現,所以這裡只要一個空殼就行了- (void) draw{}- (NSString*) getColorName:(ShapeColor)f{switch (f) {case kRedColor:return @"red";break;case kGreenColor:return @"green";break; case kBlueColor:return @"blue";break; default:return @"no clue";break;}}@end
文法有點奇怪,初次接觸,只能強迫自己忘記吧。
然後定義子類Circle
申明部分Circle.h
//// Circle.h// HelloWorld//// Created by jimmy.yang on 11-1-26.// Copyright 2011 __MyCompanyName__. All rights reserved.//#import "Shape.h"//註:因為Circle類不需要擴充其它方法,所以這裡也只要一個空殼@interface Circle : Shape {}@end
實現部分Circle.m
//// Circle.m// HelloWorld//// Created by jimmy.yang on 11-1-26.// Copyright 2011 __MyCompanyName__. All rights reserved.//#import "Circle.h"@implementation Circle-(void) draw{NSLog(@"drawing a Cirle at (%d,%d,%d,%d) in %@",bounds.x,bounds.y,bounds.width,bounds.height,[super getColorName:fillColor]);}@end
註:上面展示了在obj-C中如何調用父類的方法[super getColorName:fillColor]
子類Rectangle
//// Rectangle.h// HelloWorld//// Created by jimmy.yang on 11-1-26.// Copyright 2011 __MyCompanyName__. All rights reserved.//#import "Shape.h"@interface Rectangle : Shape {}@end
實現部分
//// Rectangle.m// HelloWorld//// Created by jimmy.yang on 11-1-26.// Copyright 2011 __MyCompanyName__. All rights reserved.//#import "Rectangle.h"@implementation Rectangle-(void) draw{NSLog(@"drawing a Rectangle at (%d,%d,%d,%d) in %@",bounds.x,bounds.y,bounds.width,bounds.height,[super getColorName:fillColor]);}@end
子類Ellipse
//// Ellipse.h// HelloWorld//// Created by jimmy.yang on 11-1-26.// Copyright 2011 __MyCompanyName__. All rights reserved.//#import "Shape.h"@interface Ellipse : Shape{}@end
實現部分
//// Ellipse.m// HelloWorld//// Created by jimmy.yang on 11-1-26.// Copyright 2011 __MyCompanyName__. All rights reserved.//#import "Ellipse.h"@implementation Ellipse-(void) draw{NSLog(@"drawing a ellipse at (%d,%d,%d,%d) in %@",bounds.x,bounds.y,bounds.width,bounds.height,[super getColorName:fillColor]);}@end
最後看下調用的主函數HelloWorld.m
#import "CommDef.h"#import "Circle.h"#import "Rectangle.h"#import "Ellipse.h"int main (int argc, const char * argv[]) { id shape[2] ;ShapeRect rect0 = {0,0,10,30};shape[0] = [Circle new];[shape[0] setBounds:rect0];[shape[0] setFillColor:kGreenColor];[shape[0] draw];ShapeRect rect1 = {0,0,40,50};shape[1] = [Rectangle new];[shape[1] setBounds:rect1];[shape[1] setFillColor:kRedColor];[shape[1] draw];ShapeRect rect2 = {0,0,30,30};shape[2] = [Ellipse new];[shape[2] setBounds:rect2];[shape[2] setFillColor:kBlueColor];[shape[2] draw]; return 0;}
註:上面的代碼中有一個id的變數,在obj-C中id相當於“任意類型”,意為指向某對象的指標(哪怕你不知道這個對象是什麼類型),總之,如果你不知道某對象的具體類型時,用它就對了。同時我們也看到了,建立一個類的執行個體用“[類 new]”來完成。
最後附上檔案結構圖: