Objective-C:继承的体现

典型的继承例子:形状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的形状基本都具有自己的坐标及其对应的属性(如半径、长、高、宽、面积、周长、体积等)。当然,视情况去定义。

      

时间: 2024-07-31 01:26:18

Objective-C:继承的体现的相关文章

面向对象的继承关系体现在数据结构上时,如何表示

现有:A, B, C三种数据类型,他们之间有一部分是相同的,可以理解为A.B.C都是一种O类型数据,也就是A.B.C都是O的一种.那么这三类数据存到关系数据库时,会采用什么方式比较高效率 . 方式1: 1)O设计为一个公用表,但是必须包含2个字段,其中1个表示数据类型,它指向A.B.C三个表中的1个: 2)A.B.C设计为数据表,分别存储三类数据,相当于O的子表. 方式2: A.B.C三个数据表都包含O中的字段,不创建O表.

C++继承实现邻接矩阵和邻接表

1.图的父类 是一个抽象类,不能实类化对象,应具有的是抽象方法,提供一个接口,在由子类继承,实现自己的方法, 应提供的共有抽象方法和保护的数据: public:     virtual bool insertVertex(const Type &v) = 0;  //插入顶点     virtual bool insertEdge(const Type &v1, const Type &v2) = 0; //插入边     virtual bool removeVertex(con

Java 面向对象之继承从哪来,为什么用,怎么用?

首先看一张图片: 现在有两个类,Student和Worker,为了提高复用性.只建立一份代码,让一个类和另一个类产生关系就可以了,这种关系就是:继承. 但我们发现这两个类没有继承关系,但是它们有共性的内容,我们可以找到它们的共享类型,→无论是学生还是工人,都是Person,Person都具备着name和age.即: 在代码中通过关键字 继承(extends),让学生和工人与Person产生关系 class Person { String name; int age; } class Studen

重新认识java(四) — 组合、聚合与继承的爱恨情仇

有人学了继承,认为他是面向对象特点之一,就在所有能用到继承的地方使用继承,而不考虑究竟该不该使用,无疑,这是错误的.那么,究竟该如何使用继承呢? java中类与类之间的关系 大部分的初学者只知道java中两个类之间可以是继承与被继承的关系,可是事实上,类之间的关系大体上存在五种-继承(实现).依赖.关联.聚合.组合. 接下来,简单的分析一下这些关系. 继承(实现) 对于类来说,这种关系叫做继承,对于接口来说,这种关系叫做实现.继承上一篇文章已经详细的讲解过了,至于实现,我想大家也都知道是怎么回事

Android(java)学习笔记118:类继承的注意事项

1 /* 2 继承的注意事项: 3 A:子类只能继承父类所有非私有的成员(成员方法和成员变量) 4 B:子类不能继承父类的构造方法,但是可以通过super(马上讲)关键字去访问父类构造方法. 5 C:不要为了部分功能而去继承 6 class A { 7 public void show1(){} 8 public void show2(){} 9 } 10 11 class B { 12 public void show2(){} 13 public void show3(){} 14 } 15

牛刀小试 - 浅析Java的继承与动态绑定

什么是继承? 继承也是面向对象的重要特性之一.顾名思义,继承就是指从已有的类中派生出新类的动作.新的类能吸收已有类的数据属性和行为,并能扩展新的能力. 而通俗一点的来说,就是指Java中可以通过继承的方式,从现有的类派生出新的类.该现有类被称为超类(父类),而派生出的新类就被称为子类(派生类). 首先,子类访问继承超类当中的所有非私有的方法和成员变量:其次,还可以在父类原有的成员的基础上添加一些新的方法和域,或者对父类的方法进行覆写(override). 所有通常也这样讲:父类是子类的一般化表现

Java:面向对象--继承

1.静态代码块和构造代码块 /* 代码块:在Java中,使用{}括起来的代码被称为代码块. 根据其位置和声明的不同,可以分为 局部代码块:局部位置,用于限定变量的生命周期. 构造代码块:在类中的成员位置,用{}括起来的代码.每次调用构造方法执行前,都会先执行构造代码块. 作用:可以把多个构造方法中的共同代码放到一起,对对象进行初始化. 静态代码块:在类中的成员位置,用{}括起来的代码,只不过它用static修饰了. 作用:一般是对类进行初始化. 面试题? 静态代码块,构造代码块,构造方法的执行顺

Java基础继承与多态

Java基础第九天 继承概述 引入 首先我来写两个代码: //定义学生类 class Student {     public void study(){ System.out.println("在教室学习"); } } //定义老师类 class Teacher { public void teach(){ System.out.println("在教室教书"); } } 我们观察上面两个代码: 发现name,age成员变量,以及getXxx()/setXxx()

关于何时用继承何时用组合

没有找到极其强烈无法辩驳的用继承的原因的时候一律用组合 组合体现为实现层面,继承主要体现在扩展方面 我觉得如果并不是需要一个类的所有东西(包括接口和属性),那么就不需要用继承,相反就用组合. 用继承那就必须是所有的都继承,不是所有的都继承,那么就是滥用继承. 组合在编译依赖和不同版本的运行时兼容性上优于继承:继承在合理复用实现的潜力优于组合 继承偏重集体,具有特殊性的集体(派生类)可以看作是一般性集体(基类)的一部分,如汽车对于交通工具. 组合偏重个体,某个功能对象被宿主对象看成自己的一部分,如