objectiveC 的OOP(上)类定义、继承及方法调用

如何改用OOP的方法来实现:

因为要用到“颜色”以及“矩形区域”二个枚举,先把他们抽出来单独放在CommDef.h文件中

然后再定义基类Shape,注意:在obj-C中,定义一个类通常分成二部分,一部分是".h"的文件,用来申明类有哪些成员(也称为类的定义文件,类似于接口),另一部分是".m"的文件,用来提供具体实现

Shape类的申明部分如下:(Shape.h)

Shape类的实现部分如下:(Shape.m)

01 //
02 //  Shape.m
03 //  HelloWorld
04 //
05 //  Created by jimmy.yang on 11-1-26.
06 //  Copyright 2011 __MyCompanyName__. All rights reserved.
07 //
08  
09 #import "Shape.h"
10  
11  
12 @implementation Shape
13  
14 - (void) setBounds:(ShapeRect)b
15 {
16     bounds = b;
17 }
18  
19 - (void) setFillColor:(ShapeColor)f
20 {
21     fillColor =f;
22 }
23  
24 //注:这个方法留给子类去实现,所以这里只要一个空壳就行了
25 - (void) draw
26 {
27      
28 }
29  
30  
31 - (NSString*) getColorName:(ShapeColor)f
32 {
33      
34     switch (f) {
35         case kRedColor:
36             return @"red";
37             break;
38         case kGreenColor:
39             return @"green";
40             break;
41         case kBlueColor:
42             return @"blue";
43             break;
44         default:
45             return @"no clue";
46             break;
47     }
48      
49 }
50  
51 @end

语法有点奇怪,初次接触,只能强迫自己忘记吧。

然后定义子类Circle

申明部分Circle.h

01 //
02 //  Circle.h
03 //  HelloWorld
04 //
05 //  Created by jimmy.yang on 11-1-26.
06 //  Copyright 2011 __MyCompanyName__. All rights reserved.
07 //
08  
09 #import "Shape.h"
10 //注:因为Circle类不需要扩展其它方法,所以这里也只要一个空壳
11 @interface Circle : Shape {
12  
13 }
14  
15 @end

实现部分Circle.m

01 //
02 //  Circle.m
03 //  HelloWorld
04 //
05 //  Created by jimmy.yang on 11-1-26.
06 //  Copyright 2011 __MyCompanyName__. All rights reserved.
07 //
08  
09 #import "Circle.h"
10  
11  
12 @implementation Circle
13  
14 -(void) draw
15 {
16     NSLog(@"drawing a Cirle at (%d,%d,%d,%d) in %@",bounds.x,bounds.y,bounds.width,bounds.height,[super getColorName:fillColor]);
17 }
18 @end

注:上面展示了在obj-C中如何调用父类的方法[super getColorName:fillColor]

子类Rectangle

01 //
02 //  Rectangle.h
03 //  HelloWorld
04 //
05 //  Created by jimmy.yang on 11-1-26.
06 //  Copyright 2011 __MyCompanyName__. All rights reserved.
07 //
08 #import "Shape.h"
09  
10 @interface Rectangle : Shape {
11  
12 }
13  
14 @end
实现部分
01 //
02 //  Rectangle.m
03 //  HelloWorld
04 //
05 //  Created by jimmy.yang on 11-1-26.
06 //  Copyright 2011 __MyCompanyName__. All rights reserved.
07 //
08  
09 #import "Rectangle.h"
10  
11  
12 @implementation Rectangle
13 -(void) draw
14 {
15     NSLog(@"drawing a Rectangle at (%d,%d,%d,%d) in %@",bounds.x,bounds.y,bounds.width,bounds.height,[super getColorName:fillColor]);
16 }
17 @end

子类Ellipse

01 //
02 //  Ellipse.h
03 //  HelloWorld
04 //
05 //  Created by jimmy.yang on 11-1-26.
06 //  Copyright 2011 __MyCompanyName__. All rights reserved.
07 //
08  
09  
10 #import "Shape.h"
11  
12 @interface Ellipse : Shape{
13  
14 }
15 @end

实现部分

01 //
02 //  Ellipse.m
03 //  HelloWorld
04 //
05 //  Created by jimmy.yang on 11-1-26.
06 //  Copyright 2011 __MyCompanyName__. All rights reserved.
07 //
08  
09 #import "Ellipse.h"
10  
11  
12 @implementation Ellipse
13 -(void) draw
14 {
15     NSLog(@"drawing a ellipse at (%d,%d,%d,%d) in %@",bounds.x,bounds.y,bounds.width,bounds.height,[super getColorName:fillColor]);
16 }
17 @end

最后看下调用的主函数HelloWorld.m

01 #import "CommDef.h"
02 #import "Circle.h"
03 #import "Rectangle.h"
04 #import "Ellipse.h"
05  
06 int main (int argc, const char * argv[]) {   
07      
08     id shape[2] ;
09      
10     ShapeRect rect0 =  {0,0,10,30};
11     shape[0] = [Circle new];
12     [shape[0] setBounds:rect0];
13     [shape[0] setFillColor:kGreenColor];
14     [shape[0] draw];   
15      
16     ShapeRect rect1 =  {0,0,40,50};
17     shape[1] = [Rectangle new];
18     [shape[1] setBounds:rect1];
19     [shape[1] setFillColor:kRedColor];
20     [shape[1] draw];
21  
22      
23     ShapeRect rect2 =  {0,0,30,30};
24     shape[2] = [Ellipse new];
25     [shape[2] setBounds:rect2];
26     [shape[2] setFillColor:kBlueColor];
27     [shape[2] draw];
28  
29      
30     return 0;
31 }

注:上面的代码中有一个id的变量,在obj-C中id相当于“任意类型”,意为指向某对象的指针(哪怕你不知道这个对象是什么类型),总之,如果你不知道某对象的具体类型时,用它就对了。同时我们也看到了,创建一个类的实例用“[类 new]”来完成。

最后附上文件结构图:

原文地址:https://www.cnblogs.com/lm3515/p/2009822.html