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

        点类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 = *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 = *PI*super.radius*super.radius;
}
-(void) Volum
{
super.volum = /*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 = *(_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 = *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: andHei:];
[rectangle Area];
[rectangle Length];
[rectangle draw];
[rectangle show];
printf("\n"); Square *square = [[Square alloc]initWith:3.3 andY:3.3 andLen: andHei:];
[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 ;
}

//运行结果

-- ::15.931 继承[:] drawing a Shape!

-- ::15.933 继承[:] drawing a point!
-- ::15.933 继承[:] x:0.0,y:0.0 -- ::15.933 继承[:] drawing a circle!
-- ::15.933 继承[:] x:1.1,y:1.1
-- ::15.933 继承[:] radius:5.0,area:78.5,Length:31.4 -- ::15.934 继承[:] drawing a Rectangle!
-- ::15.934 继承[:] x:2.2,y:2.2
-- ::15.934 继承[:] len:3.0,hei:8.0,area:24.0,length:22.0 -- ::15.934 继承[:] drawing a Square!
-- ::15.934 继承[:] x:3.3,y:3.3
-- ::15.934 继承[:] len:4.0,hei:4.0,area:16.0,length:16.0 -- ::15.935 继承[:] draw a sphere!
-- ::15.935 继承[:] x:4.4,y:4.4,z:4.4
-- ::15.935 继承[:] radius:6.0,area:452.2,volum:678.2 Program ended with exit code:

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

      

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

  1. java7-3 继承

    1.继承概述: 把多个类中相同的内容给提取出来定义到一个类中. 如何实现继承呢? Java提供了关键字:extends 格式: class 子类名 extends 父类名 {} 父类也称为基类.超类: ...

  2. c#基础学习汇总----------继承

    封装,继承,多态.这是面向对象的思想,也可以说是最基本的东西.说到继承,直接的说他就是面向对象中类与类之间的一种关系.通过继承,使得子类具有父类公有的受保护访问权限的属性和方法,同时子类可以通过加入新 ...

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

    /* 继承的注意事项: A:子类只能继承父类所有非私有的成员(成员方法和成员变量) B:子类不能继承父类的构造方法,但是可以通过super(马上讲)关键字去访问父类构造方法. C:不要为了部分功能而去 ...

  4. .NET面向对象特性之“继承”

    整体简介 1.理解继承——继承关系图 2.实现继承与接口多继承 3.new. virtual.override方法 4.抽象方法和抽象类的继承 5.继承的本质 6.继承的复用性.扩展性和安全性 7.多 ...

  5. 《java入门第一季》之面向对象(继承)

    /* 继承的注意事项: A:子类只能继承父类所有(非私有)的成员(成员方法和成员变量),私有的变量和方法没法继承 B:子类(不能)继承父类的(构造方法),但是可以通过super关键字去访问父类构造方法 ...

  6. 【Java基础】【08面向对象_继承&方法&final】

    08.01_面向对象(代码块的概述和分类)(了解)(面试的时候会问,开发不用或者很少用) A:代码块概述 在Java中,使用{}括起来的代码被称为代码块. B:代码块分类 根据其位置和声明的不同,可以 ...

  7. 08-03 java 继承

    继承格式,优缺点,概述: /* 继承概述: 把多个类中相同的内容给提取出来定义到一个类中. 如何实现继承呢? Java提供了关键字:extends 格式: class 子类名 extends 父类名 ...

  8. Java面向对象理解_代码块_继承_多态_抽象_接口

    面线对象: /* 成员变量和局部变量的区别? A:在类中的位置不同 成员变量:在类中方法外 局部变量:在方法定义中或者方法声明上 B:在内存中的位置不同 成员变量:在堆内存 局部变量:在栈内存 C:生 ...

  9. 又一次认识java(四) — 组合、聚合与继承的爱恨情仇

    有人学了继承,认为他是面向对象特点之中的一个,就在全部能用到继承的地方使用继承,而不考虑到底该不该使用,无疑.这是错误的.那么.到底该怎样使用继承呢? java中类与类之间的关系 大部分的刚開始学习的 ...

随机推荐

  1. hdoj2602 Bone Collector(DP,01背包)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2602 题意 有n块骨头,每块骨头体积为volume,价值为value,还有一个容量为v的背包,现在将骨 ...

  2. Vue中directives的用法

    关于 vue 中 directives 的用法问题,详细可以参考vue官方对directives的解释 当前文章主要讲述directives怎么用,directives做权限按钮的功能 ###1. d ...

  3. Python并发编程系列之常用概念剖析:并行 串行 并发 同步 异步 阻塞 非阻塞 进程 线程 协程

    1 引言 并发.并行.串行.同步.异步.阻塞.非阻塞.进程.线程.协程是并发编程中的常见概念,相似却也有却不尽相同,令人头痛,这一篇博文中我们来区分一下这些概念. 2 并发与并行 在解释并发与并行之前 ...

  4. 获取类的属性并排除特定属性(getType().GetProperties())

    当获取一个类型(class)的所有属性时,想排除指定属性,该如何操作? 比如:EF中一个实体类型UserEntity,通过反射获取这个类的属性时,想排除这个为映射的字段ID 使用以下方法即可! Pro ...

  5. 【SQL】185. Department Top Three Salaries

    The Employee table holds all employees. Every employee has an Id, and there is also a column for the ...

  6. [ 原创 ] Java基础2--构造方法的继承和重载

    1.构造方法的重载是指同一个类中定义不同参数的多个构造方法,已完成不同情况下对象的初始化. 例如: Point(); Point(x); Point(x,y); 2.一个类的若干个构造方法之间可以相互 ...

  7. 【DFS好题】BZOJ1999- [Noip2007]Core树网的核(数据加强版)

    NOIP的数据好水,一开始有好几个错结果NOIP数据就水过了?? [题目大意] 求无根树的直径上一段不超过S长的链,使得偏心距最小.具体概念见原题. [思路] 首先明确几个性质: (1)对于树中的任意 ...

  8. java设计模式(四)代理模式

    适用于为不同操作添加共同的额外行为.通过代理对象访问目标对象,这样可以增加对目标对象的额外操作,达到扩展目标对象功能的目的,如spring事务.AOP等. 要点:1)抽象角色:通过接口或抽象类声明真实 ...

  9. java的反射机制(第二篇)

    本文转载自:http://c.biancheng.net/cpp/html/1781.html 要理解RTTI在Java中的工作原理,首先必须知道类型信息在运行时是如何表示的,这项工作是由“Class ...

  10. 给HTML初学者的三十条最佳实践

    Nettuts +运营最困难的方面是为很多技能水平不同的用户提供服务.如果我们发布太多高级教程,我的新手用户将无法从中受益.相反也是如此.我们尽我们最大的努力,但如果你觉得你被忽略了请联系我们.这个网 ...