码迷,mamicode.com
首页 > 其他好文 > 详细

Objective-C:继承的体现

时间:2015-08-12 21:14:28      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

典型的继承例子:形状Shape为基类,继承它的类有:点类Point、圆类Circle、球体类Sphere、矩形类Rectangle、正方形类Square

        点类Point也为基类,继承它的类有:圆类Circle、球体类Sphere、矩形类Rectangle、正方形类Square

        圆类Circle也为基类,继承它的类有:球体类Sphere

        矩形类Rectangle为基类,继承它的类是:正方形类Square 

//Shape类   .h和.m文件

 1 //  Shape.h
 2 //  继承
 3 //
 4 //  Created by ma c on 15/8/11.
 5 //  Copyright (c) 2015年 bjsxt. All rights reserved.
 6 //  形状类
 7 
 8 #import <Foundation/Foundation.h>
 9 
10 @interface Shape : NSObject
11 @property(nonatomic,assign)CGFloat length;
12 @property(nonatomic,assign)CGFloat area;
13 @property(nonatomic,assign)CGFloat volum;
14 -(void)draw;
15 -(void) Area;
16 -(void) Length;
17 -(void) Volum;
18 @end
 1 //  Shape.m
 2 //  继承
 3 //
 4 //  Created by ma c on 15/8/11.
 5 //  Copyright (c) 2015年 bjsxt. All rights reserved.
 6 //  形状类
 7 
 8 #import "Shape.h"
 9 
10 @implementation Shape
11 -(void)draw
12 {
13     NSLog(@"drawing a Shape!");
14 }
15 -(void) Area{}
16 -(void) Length{}
17 -(void) Volum{}
18 @end

//Point类 .h和.m文件

 1 //  Point.h
 2 //  继承
 3 //
 4 //  Created by ma c on 15/8/11.
 5 //  Copyright (c) 2015年 bjsxt. All rights reserved.
 6 //  点类
 7 
 8 #import "Shape.h"
 9 
10 @interface MyPoint : Shape
11 @property(nonatomic,assign) CGFloat x;
12 @property(nonatomic,assign) CGFloat y;
13 -(id)initWithX:(CGFloat)m andY:(CGFloat)n;
14 -(void)show;
15 @end
 1 //  Point.m
 2 //  继承
 3 //
 4 //  Created by ma c on 15/8/11.
 5 //  Copyright (c) 2015年 bjsxt. All rights reserved.
 6 //  点类
 7 
 8 #import "MyPoint.h"
 9 
10 @implementation MyPoint
11 -(id)initWithX:(CGFloat)m andY:(CGFloat)n
12 {
13     self = [super init];
14     if(self)
15     {
16         _x = m;
17         _y = n;
18     }
19     return self;
20 }
21 -(void)draw
22 {
23     NSLog(@"drawing a point!");
24 }
25 -(void)show
26 {
27     NSLog(@"x:%.1f,y:%.1f",_x,_y);
28 }
29 @end

//圆类Circle   .h和.m文件

 1 //  Circle.h
 2 //  继承
 3 //
 4 //  Created by ma c on 15/8/12.
 5 //  Copyright (c) 2015年 bjsxt. All rights reserved.
 6 //  圆类
 7 
 8 #import "MyPoint.h"
 9 
10 @interface Circle : MyPoint
11 @property(nonatomic,assign)CGFloat radius;
12 -(id)initWithX:(CGFloat)m andY:(CGFloat)n andRadius:(CGFloat)r;
13 @end
 1 //  Circle.m
 2 //  继承
 3 //
 4 //  Created by ma c on 15/8/12.
 5 //  Copyright (c) 2015年 bjsxt. All rights reserved.
 6 //  圆类
 7 
 8 #import "Circle.h"
 9 #define PI 3.14
10 @implementation Circle
11 -(id)initWithX:(CGFloat)m andY:(CGFloat)n andRadius:(CGFloat)r
12 {
13     self = [super init];
14     if(self)
15     {
16         super.x = m;
17         super.y = n;
18         _radius = r;
19     }
20     return self;
21 }
22 -(void) Area
23 {
24     super.area = PI*_radius*_radius;
25 }
26 -(void) Length
27 {
28     super.length = 2*PI*_radius;
29 }
30 -(void)draw
31 {
32     NSLog(@"drawing a circle!");
33 }
34 -(void)show
35 {
36     [super show];
37     NSLog(@"radius:%.1f,area:%.1f,Length:%.1f",_radius,super.area,super.length);
38 }
39 @end

//球体类Sphere   .h和.m文件

 1 //  Sphere.h
 2 //  继承
 3 //
 4 //  Created by ma c on 15/8/12.
 5 //  Copyright (c) 2015年 bjsxt. All rights reserved.
 6 //  球体类
 7 
 8 #import "Circle.h"
 9 
10 @interface Sphere : Circle
11 @property(nonatomic,assign)CGFloat z;
12 -(id)initWithX:(CGFloat)m andY:(CGFloat)n andZ:(CGFloat)t andRadius:(CGFloat)r;
13 @end
 1 //  Sphere.m
 2 //  继承
 3 //
 4 //  Created by ma c on 15/8/12.
 5 //  Copyright (c) 2015年 bjsxt. All rights reserved.
 6 //  球体类
 7 
 8 #import "Sphere.h"
 9 #define PI 3.14
10 @implementation Sphere
11 @synthesize z;
12 -(id)initWithX:(CGFloat)m andY:(CGFloat)n andZ:(CGFloat)t andRadius:(CGFloat)r
13 {
14     if(self = [super init])
15     {
16         super.x = m;
17         super.y = n;
18         z = t;
19         super.radius = r;
20     }
21     return self;
22 }
23 -(void)draw
24 {
25     NSLog(@"draw a sphere!");
26 }
27 -(void) Area
28 {
29     super.area = 4*PI*super.radius*super.radius;
30 }
31 -(void) Volum
32 {
33     super.volum = 4/3*PI*super.radius*super.radius*super.radius;
34 }
35 -(void)show
36 {
37     NSLog(@"x:%.1f,y:%.1f,z:%.1f",super.x,super.y,z);
38     NSLog(@"radius:%.1f,area:%.1f,volum:%.1f",super.radius,super.area,super.volum);
39 }
40 @end

//矩形类Rectangle  .h和.m文件

 1 //  Rectangle.h
 2 //  继承
 3 //
 4 //  Created by ma c on 15/8/11.
 5 //  Copyright (c) 2015年 bjsxt. All rights reserved.
 6 //  矩形类
 7 
 8 #import "MyPoint.h"
 9 
10 @interface Rectangle : MyPoint
11 
12 @property(nonatomic,assign) CGFloat len;
13 @property(nonatomic,assign) CGFloat hei;
14 -(id)initWith:(CGFloat)m andY:(CGFloat)n andLen:(CGFloat) l andHei:(CGFloat)h;
15 @end
 1 //  Rectangle.m
 2 //  继承
 3 //
 4 //  Created by ma c on 15/8/11.
 5 //  Copyright (c) 2015年 bjsxt. All rights reserved.
 6 //  矩形类
 7 
 8 #import "Rectangle.h"
 9 
10 @implementation Rectangle
11 -(id)initWith:(CGFloat)m andY:(CGFloat)n andLen:(CGFloat) l andHei:(CGFloat)h
12 {
13     self = [super init];
14     if(self)
15     {
16         self.x = m;
17         self.y = n;
18         _len = l;
19         _hei = h;
20     }
21     return self;
22 }
23 -(void) Area
24 {
25    super.area = _len*_hei;
26 }
27 -(void) Length
28 {
29    super.length = 2*(_len+_hei);
30 }
31 -(void)draw
32 {
33     NSLog(@"drawing a Rectangle!");
34 }
35 -(void)show
36 {
37     [super show];
38     NSLog(@"len:%.1f,hei:%.1f,area:%.1f,length:%.1f",_len,_hei,super.area,super.length);
39 }
40 @end

//正方形类Square .h和.m文件

 1 //  Square.h
 2 //  继承
 3 //
 4 //  Created by ma c on 15/8/11.
 5 //  Copyright (c) 2015年 bjsxt. All rights reserved.
 6 //  正方形类
 7 
 8 #import "Rectangle.h"
 9 
10 @interface Square : Rectangle
11 @end

 

 1 //  Square.m
 2 //  继承
 3 //
 4 //  Created by ma c on 15/8/11.
 5 //  Copyright (c) 2015年 bjsxt. All rights reserved.
 6 //  正方形类
 7 
 8 #import "Square.h"
 9 
10 @implementation Square
11 -(void) Area
12 {
13     self.area = super.len*super.hei;
14 }
15 -(void) Length
16 {
17     self.length = 4*self.len;
18 }
19 -(void)draw
20 {
21     NSLog(@"drawing a Square!");
22 }
23 -(void)show
24 {
25     [super show];
26 }
27 @end

 

//主函数测试

 1 //  main.m
 2 //  继承
 3 //
 4 //  Created by ma c on 15/8/11.
 5 //  Copyright (c) 2015年 bjsxt. All rights reserved.
 6 //
 7 
 8 #import <Foundation/Foundation.h>
 9 #import "Shape.h"
10 #import "Square.h"
11 #import "MyPoint.h"
12 #import "Rectangle.h"
13 #import "Circle.h"
14 #import "Sphere.h"
15 int main(int argc, const char * argv[])
16 {
17     @autoreleasepool
18     {
19         Shape *shape = [[Shape alloc]init];
20         [shape draw];
21         printf("\n");
22         
23         
24         MyPoint *mypoint = [[MyPoint alloc]initWithX:0.0 andY:0.0];
25         [mypoint draw];
26         [mypoint show];
27         printf("\n");
28  
29         
30         Circle *circle = [[Circle alloc]initWithX:1.1 andY:1.1
31                                         andRadius:5.0];
32         [circle Area];
33         [circle Length];
34         [circle draw];
35         [circle show];
36         printf("\n");
37         
38         
39         Rectangle *rectangle = [[Rectangle alloc]initWith:2.2 andY:2.2 andLen:3 andHei:8];
40         [rectangle Area];
41         [rectangle Length];
42         [rectangle draw];
43         [rectangle show];
44         printf("\n");
45         
46         
47         Square *square = [[Square alloc]initWith:3.3 andY:3.3 andLen:4 andHei:4];
48         [square Area];
49         [square Length];
50         [square draw];
51         [square show];
52         printf("\n");
53         
54         Sphere *sphere = [[Sphere alloc]initWithX:4.4 andY:4.4 andZ:4.4 andRadius:6.0];
55         [sphere Area];
56         [sphere Volum];
57         [sphere draw];
58         [sphere show];
59         printf("\n");
60     }
61     return 0;
62 }

//运行结果

2015-08-12 18:44:15.931 继承[1875:121866] drawing a Shape!

2015-08-12 18:44:15.933 继承[1875:121866] drawing a point!
2015-08-12 18:44:15.933 继承[1875:121866] x:0.0,y:0.0

2015-08-12 18:44:15.933 继承[1875:121866] drawing a circle!
2015-08-12 18:44:15.933 继承[1875:121866] x:1.1,y:1.1
2015-08-12 18:44:15.933 继承[1875:121866] radius:5.0,area:78.5,Length:31.4

2015-08-12 18:44:15.934 继承[1875:121866] drawing a Rectangle!
2015-08-12 18:44:15.934 继承[1875:121866] x:2.2,y:2.2
2015-08-12 18:44:15.934 继承[1875:121866] len:3.0,hei:8.0,area:24.0,length:22.0

2015-08-12 18:44:15.934 继承[1875:121866] drawing a Square!
2015-08-12 18:44:15.934 继承[1875:121866] x:3.3,y:3.3
2015-08-12 18:44:15.934 继承[1875:121866] len:4.0,hei:4.0,area:16.0,length:16.0

2015-08-12 18:44:15.935 继承[1875:121866] draw a sphere!
2015-08-12 18:44:15.935 继承[1875:121866] x:4.4,y:4.4,z:4.4
2015-08-12 18:44:15.935 继承[1875:121866] radius:6.0,area:452.2,volum:678.2

Program ended with exit code: 0

注释:继承Shape的形状基本都具有自己的坐标及其对应的属性(如半径、长、高、宽、面积、周长、体积等)。当然,视情况去定义。

      

Objective-C:继承的体现

标签:

原文地址:http://www.cnblogs.com/XYQ-208910/p/4725355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!