Objective-C:继承的体现
2015-08-12 19:20
513 查看
典型的继承例子:形状Shape为基类,继承它的类有:点类Point、圆类Circle、球体类Sphere、矩形类Rectangle、正方形类Square
点类Point也为基类,继承它的类有:圆类Circle、球体类Sphere、矩形类Rectangle、正方形类Square
圆类Circle也为基类,继承它的类有:球体类Sphere
矩形类Rectangle为基类,继承它的类是:正方形类Square
//Shape类 .h和.m文件
//Point类 .h和.m文件
//圆类Circle .h和.m文件
//球体类Sphere .h和.m文件
//矩形类Rectangle .h和.m文件
//正方形类Square .h和.m文件
//主函数测试
//运行结果
注释:继承Shape的形状基本都具有自己的坐标及其对应的属性(如半径、长、高、宽、面积、周长、体积等)。当然,视情况去定义。
点类Point也为基类,继承它的类有:圆类Circle、球体类Sphere、矩形类Rectangle、正方形类Square
圆类Circle也为基类,继承它的类有:球体类Sphere
矩形类Rectangle为基类,继承它的类是:正方形类Square
//Shape类 .h和.m文件
// Shape.h // 继承 // // Created by ma c on 15/8/11. // Copyright (c) 2015年. All rights reserved. // 形状类 #import <Foundation/Foundation.h> @interface Shape : NSObject @property(nonatomic,assign)CGFloat length; @property(nonatomic,assign)CGFloat area; @property(nonatomic,assign)CGFloat volum; -(void)draw; -(void) Area; -(void) Length; -(void) Volum; @end
// Shape.m // 继承 // // Created by ma c on 15/8/11. // Copyright (c) 2015年. All rights reserved. // 形状类 #import "Shape.h" @implementation Shape -(void)draw { NSLog(@"drawing a Shape!"); } -(void) Area{} -(void) Length{} -(void) Volum{} @end
//Point类 .h和.m文件
// Point.h // 继承 // // Created by ma c on 15/8/11. // Copyright (c) 2015年. All rights reserved. // 点类 #import "Shape.h" @interface MyPoint : Shape @property(nonatomic,assign) CGFloat x; @property(nonatomic,assign) CGFloat y; -(id)initWithX:(CGFloat)m andY:(CGFloat)n; -(void)show; @end
// Point.m // 继承 // // Created by ma c on 15/8/11. // Copyright (c) 2015年. All rights reserved. // 点类 #import "MyPoint.h" @implementation MyPoint -(id)initWithX:(CGFloat)m andY:(CGFloat)n { self = [super init]; if(self) { _x = m; _y = n; } return self; } -(void)draw { NSLog(@"drawing a point!"); } -(void)show { NSLog(@"x:%.1f,y:%.1f",_x,_y); } @end
//圆类Circle .h和.m文件
// Circle.h // 继承 // // Created by ma c on 15/8/12. // Copyright (c) 2015年. All rights reserved. // 圆类 #import "MyPoint.h" @interface Circle : MyPoint @property(nonatomic,assign)CGFloat radius; -(id)initWithX:(CGFloat)m andY:(CGFloat)n andRadius:(CGFloat)r; @end
// Circle.m // 继承 // // Created by ma c on 15/8/12. // Copyright (c) 2015年. All rights reserved. // 圆类 #import "Circle.h" #define PI 3.14 @implementation Circle -(id)initWithX:(CGFloat)m andY:(CGFloat)n andRadius:(CGFloat)r { self = [super init]; if(self) { super.x = m; super.y = n; _radius = r; } return self; } -(void) Area { super.area = PI*_radius*_radius; } -(void) Length { super.length = 2*PI*_radius; } -(void)draw { NSLog(@"drawing a circle!"); } -(void)show { [super show]; NSLog(@"radius:%.1f,area:%.1f,Length:%.1f",_radius,super.area,super.length); } @end
//球体类Sphere .h和.m文件
// Sphere.h // 继承 // // Created by ma c on 15/8/12. // Copyright (c) 2015年. All rights reserved. // 球体类 #import "Circle.h" @interface Sphere : Circle @property(nonatomic,assign)CGFloat z; -(id)initWithX:(CGFloat)m andY:(CGFloat)n andZ:(CGFloat)t andRadius:(CGFloat)r; @end
// Sphere.m // 继承 // // Created by ma c on 15/8/12. // Copyright (c) 2015年. All rights reserved. // 球体类 #import "Sphere.h" #define PI 3.14 @implementation Sphere @synthesize z; -(id)initWithX:(CGFloat)m andY:(CGFloat)n andZ:(CGFloat)t andRadius:(CGFloat)r { if(self = [super init]) { super.x = m; super.y = n; z = t; super.radius = r; } return self; } -(void)draw { NSLog(@"draw a sphere!"); } -(void) Area { super.area = 4*PI*super.radius*super.radius; } -(void) Volum { super.volum = 4/3*PI*super.radius*super.radius*super.radius; } -(void)show { NSLog(@"x:%.1f,y:%.1f,z:%.1f",super.x,super.y,z); NSLog(@"radius:%.1f,area:%.1f,volum:%.1f",super.radius,super.area,super.volum); } @end
//矩形类Rectangle .h和.m文件
// Rectangle.h // 继承 // // Created by ma c on 15/8/11. // Copyright (c) 2015年. All rights reserved. // 矩形类 #import "MyPoint.h" @interface Rectangle : MyPoint @property(nonatomic,assign) CGFloat len; @property(nonatomic,assign) CGFloat hei; -(id)initWith:(CGFloat)m andY:(CGFloat)n andLen:(CGFloat) l andHei:(CGFloat)h; @end
// Rectangle.m // 继承 // // Created by ma c on 15/8/11. // Copyright (c) 2015年. All rights reserved. // 矩形类 #import "Rectangle.h" @implementation Rectangle -(id)initWith:(CGFloat)m andY:(CGFloat)n andLen:(CGFloat) l andHei:(CGFloat)h { self = [super init]; if(self) { self.x = m; self.y = n; _len = l; _hei = h; } return self; } -(void) Area { super.area = _len*_hei; } -(void) Length { super.length = 2*(_len+_hei); } -(void)draw { NSLog(@"drawing a Rectangle!"); } -(void)show { [super show]; NSLog(@"len:%.1f,hei:%.1f,area:%.1f,length:%.1f",_len,_hei,super.area,super.length); } @end
//正方形类Square .h和.m文件
// Square.h // 继承 // // Created by ma c on 15/8/11. // Copyright (c) 2015年. All rights reserved. // 正方形类 #import "Rectangle.h" @interface Square : Rectangle @end
// Square.m // 继承 // // Created by ma c on 15/8/11. // Copyright (c) 2015年. All rights reserved. // 正方形类 #import "Square.h" @implementation Square -(void) Area { self.area = super.len*super.hei; } -(void) Length { self.length = 4*self.len; } -(void)draw { NSLog(@"drawing a Square!"); } -(void)show { [super show]; } @end
//主函数测试
// main.m // 继承 // // Created by ma c on 15/8/11. // Copyright (c) 2015年. All rights reserved. // #import <Foundation/Foundation.h> #import "Shape.h" #import "Square.h" #import "MyPoint.h" #import "Rectangle.h" #import "Circle.h" #import "Sphere.h" int main(int argc, const char * argv[]) { @autoreleasepool { Shape *shape = [[Shape alloc]init]; [shape draw]; printf("\n"); MyPoint *mypoint = [[MyPoint alloc]initWithX:0.0 andY:0.0]; [mypoint draw]; [mypoint show]; printf("\n"); Circle *circle = [[Circle alloc]initWithX:1.1 andY:1.1 andRadius:5.0]; [circle Area]; [circle Length]; [circle draw]; [circle show]; printf("\n"); Rectangle *rectangle = [[Rectangle alloc]initWith:2.2 andY:2.2 andLen:3 andHei:8]; [rectangle Area]; [rectangle Length]; [rectangle draw]; [rectangle show]; printf("\n"); Square *square = [[Square alloc]initWith:3.3 andY:3.3 andLen:4 andHei:4]; [square Area]; [square Length]; [square draw]; [square show]; printf("\n"); Sphere *sphere = [[Sphere alloc]initWithX:4.4 andY:4.4 andZ:4.4 andRadius:6.0]; [sphere Area]; [sphere Volum]; [sphere draw]; [sphere show]; printf("\n"); } return 0; }
//运行结果
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的形状基本都具有自己的坐标及其对应的属性(如半径、长、高、宽、面积、周长、体积等)。当然,视情况去定义。
相关文章推荐
- 我的iOS开发之Objective-C第5、6天之Founcation
- ES5 object方法整理
- Objective C ARC 使用及原理
- Objective-C编码规范:26个方面解决iOS开发问题
- dynamic调用时报RuntimeBinderException:“object”未包含“xxx”的定义 错误
- Objective-C之UISearchController
- Objective-C:分类(Category、extension)
- 我的iOS开发之Objective-C第4天之属性、复合
- java的eclipse操作和常用类Object的使用
- Object.wait()
- Objective-C:协议protocol
- Objective-C修饰符const
- JSONArray和JSONObject的简单使用
- ArcObject IFeature set_Shape()和Delete()报错
- Objective-C之run loop详解
- iOS规范化时间格式,object-C计算指定时间与当前的时间差
- 我的iOS开发之Objective-C第2天之面向对象特性之封装
- 关于Object类中equals方法的重写
- Objective-C Modernization Tool
- Objective-C——判断对象等同性