博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Objective-C:继承的体现
阅读量:6277 次
发布时间:2019-06-22

本文共 8408 字,大约阅读时间需要 28 分钟。

典型的继承例子:形状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年. All rights reserved. 6 //  形状类 7  8 #import 
9 10 @interface Shape : NSObject11 @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年. All rights reserved. 6 //  形状类 7  8 #import "Shape.h" 9 10 @implementation Shape11 -(void)draw12 {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年. All rights reserved. 6 //  点类 7  8 #import "Shape.h" 9 10 @interface MyPoint : Shape11 @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年. All rights reserved. 6 //  点类 7  8 #import "MyPoint.h" 9 10 @implementation MyPoint11 -(id)initWithX:(CGFloat)m andY:(CGFloat)n12 {13     self = [super init];14     if(self)15     {16         _x = m;17         _y = n;18     }19     return self;20 }21 -(void)draw22 {23     NSLog(@"drawing a point!");24 }25 -(void)show26 {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年. All rights reserved. 6 //  圆类 7  8 #import "MyPoint.h" 9 10 @interface Circle : MyPoint11 @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年. All rights reserved. 6 //  圆类 7  8 #import "Circle.h" 9 #define PI 3.1410 @implementation Circle11 -(id)initWithX:(CGFloat)m andY:(CGFloat)n andRadius:(CGFloat)r12 {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) Area23 {24     super.area = PI*_radius*_radius;25 }26 -(void) Length27 {28     super.length = 2*PI*_radius;29 }30 -(void)draw31 {32     NSLog(@"drawing a circle!");33 }34 -(void)show35 {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年. All rights reserved. 6 //  球体类 7  8 #import "Circle.h" 9 10 @interface Sphere : Circle11 @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年. All rights reserved. 6 //  球体类 7  8 #import "Sphere.h" 9 #define PI 3.1410 @implementation Sphere11 @synthesize z;12 -(id)initWithX:(CGFloat)m andY:(CGFloat)n andZ:(CGFloat)t andRadius:(CGFloat)r13 {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)draw24 {25     NSLog(@"draw a sphere!");26 }27 -(void) Area28 {29     super.area = 4*PI*super.radius*super.radius;30 }31 -(void) Volum32 {33     super.volum = 4/3*PI*super.radius*super.radius*super.radius;34 }35 -(void)show36 {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年. All rights reserved. 6 //  矩形类 7  8 #import "MyPoint.h" 9 10 @interface Rectangle : MyPoint11 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年. All rights reserved. 6 //  矩形类 7  8 #import "Rectangle.h" 9 10 @implementation Rectangle11 -(id)initWith:(CGFloat)m andY:(CGFloat)n andLen:(CGFloat) l andHei:(CGFloat)h12 {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) Area24 {25    super.area = _len*_hei;26 }27 -(void) Length28 {29    super.length = 2*(_len+_hei);30 }31 -(void)draw32 {33     NSLog(@"drawing a Rectangle!");34 }35 -(void)show36 {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年. All rights reserved. 6 //  正方形类 7  8 #import "Rectangle.h" 9 10 @interface Square : Rectangle11 @end

 

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

 

//主函数测试

1 //  main.m 2 //  继承 3 // 4 //  Created by ma c on 15/8/11. 5 //  Copyright (c) 2015年. All rights reserved. 6 // 7  8 #import 
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 @autoreleasepool18 {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.131 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.02015-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.12015-08-12 18:44:15.933 继承[1875:121866] radius:5.0,area:78.5,Length:31.42015-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.22015-08-12 18:44:15.934 继承[1875:121866] len:3.0,hei:8.0,area:24.0,length:22.02015-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.32015-08-12 18:44:15.934 继承[1875:121866] len:4.0,hei:4.0,area:16.0,length:16.02015-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.42015-08-12 18:44:15.935 继承[1875:121866] radius:6.0,area:452.2,volum:678.2Program ended with exit code: 0

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

      

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4725355.html,如需转载请自行联系原作者
你可能感兴趣的文章
Java中AES加密解密以及签名校验
查看>>
定义内部类 继承 AsyncTask 来实现异步网络请求
查看>>
VC中怎么读取.txt文件
查看>>
七天学会ASP.NET MVC (四)——用户授权认证问题
查看>>
Python 定制类的特殊方法与授权
查看>>
回溯法求解数独算法(C语言)
查看>>
如何清理mac系统垃圾
查看>>
企业中最佳虚拟机软件应用程序—Parallels Deskto
查看>>
送给“正在纠结”、“准备纠结”的前端开发们
查看>>
Android 自定义Camera基本使用步骤及关键注意点
查看>>
Nginx配置文件详细说明
查看>>
sgu 127
查看>>
如何在Navicat中设置HTTP属性
查看>>
怎么用Navicat Premium图标编辑器创建表
查看>>
谈DELL收购EMC
查看>>
SpringBoot 异常:Target object must not be null
查看>>
监控系统那些事儿01——好奇,了解HP Openview系列软件
查看>>
Spring配置文件(2)配置方式
查看>>
获取客户端网卡MAC地址和IP地址实现JS代码
查看>>
CentOS安装Node.js
查看>>