继承

  面向对象编程 (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. LeetCode 25. K 个一组翻转链表 | Python

    25. K 个一组翻转链表 题目来源:https://leetcode-cn.com/problems/reverse-nodes-in-k-group 题目 给你一个链表,每 k 个节点一组进行翻转 ...

  2. Android Library 发布开源库 JCenter & JitPack 攻略

    对于Android 的开源库,一般通过 JCenter 或者 JitPack 发布开源.两种方式均可~ 当你造了一个好玩有用的东西想要分享给大家时,开源出来便是一种好方式~ 一. 上传开源库到 JCe ...

  3. Jenkins+Ansible+Gitlab自动发布/回滚Spring项目

    一.实现方法流程图 流程图如下:代码托管在本地GitLab上(为了复现整套流水线,我直接使用了GitHub,懒得再搭建GitLab),开发完成后提交代码到代码仓库,[自动]触发Jenkins进行持续集 ...

  4. Linux时间的相关的操作

    时间(修改时区,修改时间,同步网络时间) 查看当前系统时间 date 修改时区 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 修改当前系统时间 ...

  5. Prime Path素数筛与BFS动态规划

    埃拉托斯特尼筛法(sieve of Eratosthenes ) 是古希腊数学家埃拉托斯特尼发明的计算素数的方法.对于求解不大于n的所有素数,我们先找出sqrt(n)内的所有素数p1到pk,其中k = ...

  6. 李婷华 201771010113 《面向对象程序设计(java)》 第二周学习总结

    第一部分:理论知识学习部分 第三章 java的基本程序设计结构 本章主要学习数据类型.变量.运算符.类型转换.字符串.输入输出.控制流程.大数值.数组等内容. 1.基本知识 (1)标识符:由字母.下划 ...

  7. uniapp自定义简单弹窗组件

    2.0(2019-08-31) 船新版本的信息弹窗组件 插件市场地址:http://ext.dcloud.net.cn/plugin?id=672 可以弹出很多条信息,并单独控制消失时间.点击消失. ...

  8. Openwrt:添加"自定义软件包.ipk"

    我们已经尝试做的一件事情,是让移植软件到OpenWrt的操作变得非常容易.如果打开OpenWrt里的一个软件包的目录(OpenWrt/Package/* 或 OpenWrt/feeds/package ...

  9. 构建自己的专用OpenCV----记录一次由applyColorMap()引发的探索

    在编写实际项目的过程中,我需要实现绿色主题的"伪彩色"变换.在目前提供的模板中,只有summer最为接近,但是它的颜色太浅了,看上去不是很清晰.所以我结合ocean和summer两 ...

  10. 真香警告!扩展 swagger支持文档自动列举所有枚举值

    承接上篇文章 <一站式解决使用枚举的各种痛点> 文章最后提到:在使用 swagger 来编写接口文档时,需要告诉前端枚举类型有哪些取值,每次增加取值之后,不仅要改代码,还要找到对应的取值在 ...