继承

  面向对象编程 (OOP) 语言的一个主要功能就是"继承"。继承是指这样一种能力:它可以使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展。通过继承创建的新类称为"子类"或"派生类",被继承的类称为"基类"、"父类"或"超类"。继承的过程,就是从一般到特殊的过程。在考虑使用继承时,有一点需要注意,那就是两个类之间的关系应该是"属于"关系。例如,Teacher 是一个人,Student 也是一个人,因此这两个类都可以继承 Person 类。但是 Leg 类却不能继承 Person 类,因为腿并不是一个人。

Person类

/////////////////    .h    ////////////////
#import <Foundation/Foundation.h> @interface Person : NSObject @property (nonatomic,copy)NSString *name;
@property (nonatomic,assign)int age;
@property (nonatomic,assign)NSString *sex; - (void)printInfo;
@end ///////////////// .m ////////////////
#import "Person.h" @implementation Person
@synthesize name = _name,sex = _sex;
@synthesize age = _age; - (void)printInfo {
NSLog(@"我的名字叫:%@ 今年%d岁 我是一名%@ %@",self.name,self.age,self.sex,NSStringFromClass([self class]));
}
@end

  

  

Teacher 类

/////////////    .h   ////////////
#import "Person.h" @interface Teacher : Person @end ///////////// .m ////////////
#import "Teacher.h" @implementation Teacher - (void)printInfo {
NSLog(@"我的名字叫:%@ 今年%d岁 我是一名%@ %@",self.name,self.age,self.sex,NSStringFromClass([self class]));
}
@end

  

封装
  封装是对象和类概念的主要特性。它是隐藏内部实现,稳定外部接口,可以看作是"包装"。 封装,也就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏。

Student 类

////////////////    .h    ///////////
#import "Person.h" @interface Student : Person - (id)initWithName:(NSString *)name sex:(NSString *)sex age:(int)age;
@end //////////////// .m ///////////
#import "Student.h" @implementation Student - (id)initWithName:(NSString *)name sex:(NSString *)sex age:(int)age
{
self = [super init];
if (self) {
self.name = name;
self.sex = sex;
self.age = age;
}
return self;
} - (void)printInfo {
NSLog(@"我的名字叫:%@ 今年%d岁 我是一名%@ %@",self.name,self.age,self.sex,NSStringFromClass([self class]));
}
@end

  

多态
  多态性(polymorphism)是允许你将父对象设置成为和一个或更多的他的子对象相等的技术,赋值之后,父对象就可以根据当前赋值给它的子对象的特性以不同的方式运作。简单的说,就是一句话:允许将子类类型的指针赋值给父类类型的指针。不同对象以自己的方式响应相同的消息的能力叫做多态。意思就是假设生物类(life)都用有一个相同的 方法-eat;
那人类属于生物,猪也属于生物,都继承了life后,实现各自的eat,但是调用是我们只需调用各自的eat方法。
也就是不同的对象以 自己的方式响应了相同的消息(响应了eat这个选择器)。
实现多态,有二种方式,覆盖(是指子类重新定义父类的虚函数的做法),重载(是指允许存在多个同名函数,而这些函数的参数表不同(或许参数个数不同,或许参数类型不同,或许两者都不同))。

Cleaner 类

///////////////////    .h    //////////
#import "Person.h" @interface Cleaner : Person @end /////////////////// .m //////////
#import "Cleaner.h" @implementation Cleaner - (void)printInfo {
NSLog(@"我的名字叫:%@ 今年%d岁 我是一名%@ %@",self.name,self.age,self.sex,NSStringFromClass([self class]));
} - (void)work {
NSLog(@"我在扫地");
} - (void)work:(NSString *)tool {
NSLog(@"我在用%@扫地",tool);
}
@end

AppDelegate.m 中测试

#import "AppDelegate.h"
#import "Teacher.h"
#import "Student.h"
#import "Person.h"
#import "Cleaner.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { Person *p = [[Person alloc] init];
p.name = @"隔壁老王";
p.age = 36;
p.sex = @"男"; Teacher *t = [[Teacher alloc] init];
t.name = @"CJK";
t.age = 28;
t.sex = @"女"; Student *s = [[Student alloc] initWithName:@"小明" sex:@"男" age:12]; Cleaner *c = [[Cleaner alloc] init];
c.name = @"程燕飞";
c.age = 48;
c.sex = @"女"; [p printInfo];
[t printInfo];
[s printInfo];
[c printInfo];
[c work];
[c work:@"魔法扫帚"]; return YES;
} - (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
} - (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
} - (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
} - (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
} - (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
} @end

测试结果:

2015-06-01 17:50:34.174 Attendance[54400:1327317] 我的名字叫:隔壁老王今年36我是一名男 Person

2015-06-01 17:50:34.175 Attendance[54400:1327317] 我的名字叫:CJK 今年28我是一名女 Teacher

2015-06-01 17:50:34.175 Attendance[54400:1327317] 我的名字叫:小明今年12我是一名男 Student

2015-06-01 17:50:34.175 Attendance[54400:1327317] 我的名字叫:程燕飞今年48我是一名女 Cleaner

2015-06-01 17:50:34.175 Attendance[54400:1327317] 我在扫地

2015-06-01 17:50:34.175 Attendance[54400:1327317] 我在用魔法扫帚扫地

本站文章为宝宝巴士 SD.Team原创,转载务必在明显处注明:(作者官方网站:宝宝巴士
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4544543.html

[Objective-C] 004_继承封装与多态的更多相关文章

  1. JavaScript基础--面向对象三大特性(八):继承封装多态

    一.构造函数基本用法:function 类名(参数列表){ 属性=参数值} function Person(name,age){ this.name = name; this.age = age; } ...

  2. Java的继承、封装与多态

    Java的继承.封装与多态 基本概念 面向对象OO(Object Oriented):把数据及对数据的操作方法放在一起,作为一个相互依存的整体,即对象. 对同类对象抽象出共性,即类. 比如人就是一个类 ...

  3. 第32节:Java中-构造函数,静态方法,继承,封装,多态,包

    构造函数实例 class Cat{ // 设置私有的属性 name private String name; // 设置name的方法 public void setName(String Name) ...

  4. 【Java基本功】一文了解Java中继承、封装、多态的细节

    本节主要介绍Java面向对象三大特性:继承 封装 多态,以及其中的原理. 本文会结合虚拟机对引用和对象的不同处理来介绍三大特性的原理. 继承 Java中的继承只能单继承,但是可以通过内部类继承其他类来 ...

  5. JavaSE(二)之继承、封装、多态

    学习完类与对象终于认识到什么是类,什么是对象了.接下来要看的就是java的三大特征:继承.封装.多态. 一.封装(数据的隐藏) 在定义一个对象的特性的时候,有必要决定这些特性的可见性,即哪些特性对外部 ...

  6. PHP面向对象详解:继承、封装与多态

    首先,在解释面向对象之前先解释下什么是面向对象? [面向对象]1.什么是类? 具有相同属性(特征)和方法(行为)的一系列个体的集合,类是一个抽象的概念2.什么是对象?从类中拿到的具有具体属性值得个体, ...

  7. python的面向对象的特性(继承、封装、多态)

    创建自已对象就python非常核心的概念,事实上,python被称为面向对象语言,本章会介绍如何创建对象.以及面向对象的概念:继承.封装.多态. 多态: 可对不同类的对象使用同样的操作. 封装:对外部 ...

  8. OOP面向对象 三大特征 继承封装多态

    OOP面向对象 ----三大特征 继承封装多态 面向对象(Object Oriented,OO)是软件开发方法.面向对象的概念和应用已超越了程序设计和软件开发,扩展到如数据库系统.交互式界面.应用结构 ...

  9. Java中的继承、封装、多态的理解

    Java中的继承.封装.多态 继承的理解: 1.继承是面向对象的三大特征之一,也是实现代码复用的重要手段.Java的继承具有单继承的特点,每个子类只有一个直接父类. 2.Java的继承通过extend ...

随机推荐

  1. 网络流--最大流--POJ 2139(超级源汇+拆点建图+二分+Floyd)

    Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...

  2. csp-j2019游记

    我一pj蒟蒻这点水平还来写游记? 算了,毕竟是第一次,记录一下吧 noip->csp 话说我跟竞赛是不是天生八字不合啊...... 小学的时候学小奥,等我开始报名比赛,当时似乎所有竞赛都被叫停了 ...

  3. JS做类型检测到底有几种方法?看完本文就知道了!

    JS有很多数据类型,对于不同数据类型的识别和相互转换也是面试中的一个常考点,本文主要讲的就是类型转换和类型检测. 数据类型 JS中的数据类型主要分为两大类:原始类型(值类型)和引用类型.常见的数据类型 ...

  4. 一文教你快速搞懂速度曲线规划之S形曲线(超详细+图文+推导+附件代码)

    本文介绍了运动控制终的S曲线,通过matlab和C语言实现并进行仿真:本文篇幅较长,请自备茶水: 请帮忙点个赞

  5. OpenCV 经纬法将鱼眼图像展开

    文章目录 前言 理论部分 鱼眼展开流程 鱼眼标准坐标计算 标准坐标系与球坐标的转换 代码实现 测试效果如下图 总结 this demo on github 前言 鱼眼镜头相比传统的镜头,视角更广,采集 ...

  6. Mybatis 分页:Pagehelper + 拦截器实现

    一.分页插件 Pagehelper PageHelper是Mybatis的一个分页插件,非常好用! 1.1 Spring Boot 依赖 <!-- pagehelper 分页插件--> & ...

  7. Badboy录制脚本时,提示脚本错误的解决方法

    如下,录制时发生错误:

  8. [hdu3507 Print Article]斜率优化dp入门

    题意:需要打印n个正整数,1个数要么单独打印要么和前面一个数一起打印,1次打印1组数的代价为这组数的和的平方加上常数M.求最小代价. 思路:如果令dp[i]为打印前i个数的最小代价,那么有 dp[i] ...

  9. 选择函数index_select

    书中(pytorch入门实战)讲:index_select(input, dim, index),指定维度dim上选取,未有示例. 查到相关资料后, import torch as t # 导入tor ...

  10. vue 在main.js里使用vue实例

    可以用 Vue.prototype 比如 Vue.prototype.$indicator.close(); 关闭正在加载的动画