Python基础编程.ppt_第1页
Python基础编程.ppt_第2页
Python基础编程.ppt_第3页
Python基础编程.ppt_第4页
Python基础编程.ppt_第5页
已阅读5页,还剩36页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1 PythonOOP基础周鹏 课程目标 了解PythonOOP特性简单讲解应用场景 B2BSA 简单讲解编程思路 3 本课目标 熟悉对象和类的基本概念 对象和类 什么是对象 一切皆对象 1 String L 2 3 ist x 1 y 2 deffunc x func importmodulefileclass ObjectOrientedProgramming OOP 对象和类 什么是类及其实例 例如 狗是一个 类 则一只叫 旺财 的狗就是这个类的一个 实例 classinstance一个类可以有无数个实例 对象和类 类和实例的组成 属性attributes方法methods 即类或实例中的函数问 以狗为例 属性有哪些属性 又可以有哪些方法 属性 嘴 爪子 尾巴 方法 叫 跑 咬 术语composite has a 对象和类 例 classdog def init self self mouth big defbark self print sWANG aWANG a self mouth help dog dir dog wangcai dog help wangcai dir wangcai wangcai bark dog bark dog bark wangcai 说明什么 对象和类 说明类定义需要指定构造方法 init 当执行wangcai dog 时实际上是执行了dog init wangcai 这个方法 函数 如果不指定 所有的方法必须指定一个默认的参数self作为第一个参数 如果不指定 类定义本身也是对象 因此可直接调用 如前所示在类中对自身属性的引用必须显式的使用self 否则 对象和类 例 printdog mouth printwangcai mouth printdog bark printwangcai bark 说明什么 如果要定义在类中默认存在的属性 对象和类 定义类的属性 classdog jaw sharp 32 paw long 5 def init self printdog jaw xiaohei dog print join str x forxinxiaohei paw 对象和类 方法的参数练习 定义一个dog类 包含jaw paw mouth等属性以及bark escape bite 和eat 等动作 以及reaction 动作 reaction对各种外部刺激做出反应 如攻击 attack 则吠叫并逃跑 食物则吃 人则吠叫并咬 用字符串表示刺激动作 定义一个实例xiaohuang 实际运行这些反应 反应时必须与对应的属性匹配 例如咬必须使用mouth等 对象和类 型与类型type类class型就是系统默认内置的类 如 type 1 2 3 type list 或者说 类就是型的一种 对象和类 型与类 d dog type d d type dog type classobj Traceback mostrecentcalllast File line1 in NameError name classobj isnotdefined 对象和类 型与类 type object type list type type 继承 基本概念狗腊肠狗哈巴狗一只叫 旺财 的 土狗 继承 定义子类classDog def init self self sound wang wang defbark self printself soundclassHusky Dog def init self self sound woo wooo niub Husky niub bark 继承 定义子类到目前为止 可以看到OOP的哪些好处 封装 代码复用 继承 定义子类classSharpei Dog def init self self sound ao ao suobi Sharpei suobi bark 此时运行niub bark 说明 子类之间的属性不会互相影响 继承 定义子类如果执行如下语句 suobi1 Sharpei suobi1 sound kao kao 此时再执行soubi bark 说明 实例之间的属性也不会互相影响 继承 更复杂的复用例子classDog object def init self self sound wang wang self paw sharp self jaw long defbark classSharpei Dog def init self Dog init self self sound ao ao 继承 类 子类和实例的关系术语inheritance is a沙皮狗is a狗suobiis a沙皮狗比较composition has a 属性也可以是一个类的实例 instance 继承 类 子类和实例的关系 继承 名字空间在类中的表现classDog paw Nonedef init self self sound wang wang self fur print ThisisaDog s self sounddefgrow self self teeth white 继承 名字空间在类中的表现classHusky Dog brain 0 9def init self Dog init self super Husky self init SAMEEFFECTBUTBETTERself sound woo wooo self fur print ThisisaHusky s fur s self sound self fur defgrow self self paw greyfold self teeth whitesharp self fur greylong defwork self self paw whiteunfold self muscle 0 9 继承 名字空间在类中的表现print ntest1links print niub dict s niub dict print niub class s niub class print Husky bases s Husky bases print ntest2namespace print niub dict s niub dict print Husky dict s Husky dict print Dog dict s Dog dict print ntest3 dict changes niub grow print aftergrow print tniub dict s niub dict print tHusky dict s niub dict print tDog dict s Dog dict niub work print whenwork print tniub dict s niub dict print tHusky dict s niub dict print tDog dict s Dog dict 继承 名字空间在类中的表现结果说明 继承 类 子类和实例的关系 继承 名字空间在类中的表现 dict 是内置的属性本质 目录索引使用 class 和 bases 向上链接 继承 名字空间在类中的表现如果改变了 dict 的值 一个C中名字覆盖的例子 C fork Python os fork 继承 运算符重载什么是运算符 a b x 1printx add 2 y 2y mul 3 继承 运算符重载x Husky y Husky x y classHusky Dog def add self baby Husky returnbaby 函数 方法 重载 继承 运算符重载运算符举例 add radd mul iadd str repr and or getattr setattr getitem setitem len cmp lt eq gt iter 迭代器 回顾 call 多态 概念Polymophism例如 1 2 a b x Husky y Husky x y 多态 概念多态即对方法的重载在Java C 中 方法的重载必须先进行继承 而在Python中 可以在两个不相干的类中定义同样的方法接口 从而实现多态性回顾前面对Husky和Shapei类的bark 方法的定义 多态 一个例子 FakeFile回顾 文件重定向 buffer classFakeFile defwrite self string buffer append string importsys stdout sys stdoutsys stdout FakeFile printsomesthi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论