面向对象编程模式

对象:独立的存在 或 作为目标的事物
独立性:对象都存在清晰的边界,重点在于划分边界
功能性:对象都能表现出一些功能、操作或行为
交互性:对象之间存在交互,如:运算和继承
万物皆对象

面向对象编程思想

image.png
OOP本质是把问题解决抽象为以对象为中心的计算机程序
把对象当作程序的基本单元,对象包含数据操作数据的函数
面向对象编程 重点在于,高抽象的复用代码
是一种编程方式,并非解决问题的高级方法
面向过程VS面向对象
image.png

面向对象三个特征

封装:
image.png
image.png
继承:
image.png
image.png
多态:
image.png
image.png
都是表达抽象和复用

Python面向对象术语

  1. ![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1603593674598-0ea6baaf-ef44-4023-992a-f3d624e56767.png#align=left&display=inline&height=121&margin=%5Bobject%20Object%5D&name=image.png&originHeight=242&originWidth=1095&size=144059&status=done&style=none&width=547.5)<br /> ![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1603593717731-aa8d7059-4b4c-4a0f-8245-bf2b2beae3d5.png#align=left&display=inline&height=167&margin=%5Bobject%20Object%5D&name=image.png&originHeight=334&originWidth=1164&size=135050&status=done&style=none&width=582)<br />类对象VS实例对象:<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1603593782951-e8d32938-c32e-4867-976d-3be95621c4d7.png#align=left&display=inline&height=133&margin=%5Bobject%20Object%5D&name=image.png&originHeight=266&originWidth=1291&size=150258&status=done&style=none&width=645.5)<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1603593812630-09f7c7dc-6115-470e-840c-d80d523af989.png#align=left&display=inline&height=329&margin=%5Bobject%20Object%5D&name=image.png&originHeight=658&originWidth=1367&size=343653&status=done&style=none&width=683.5)

实例

  1. class Product:
  2. def __init__(self,name):
  3. self.name = name
  4. self.label_price = 0
  5. self.real_price = 0
  6. c = Product("电脑")
  7. d = Product("打印机")
  8. e = Product("投影仪")
  9. c.label_price,c.real_price = 10000,8000
  10. d.label_price,d.real_price = 2000,1500
  11. e.label_price,e.real_price = 1500,900
  12. s1,s2 = 0,0
  13. for i in [c,d,e]:
  14. s1 += i.label_price
  15. s2 += i.real_price
  16. print(s1,s2)

Python类的构建

(1)类的基本构建

image.png
类对象:
image.png
类对象内直接包含的语句会被执行,因此,一般不直接在类定义中直接包含语句
实例对象:
image.png

(2)类的构造函数

image.png
通过构造函数,从类生成对象
image.png

(3)类的属性

类属性:类对象的属性,由所有实例对象所共享
image.png
实例属性:实例对象的属性,由各实例对象所共享
image.png
image.png

(4)类的方法

image.png

  1. 实例方法:类内部定义的函数,与实例对象相关

image.png
image.png

  1. 类方法:与类对象相关的函数,由所有的实例对象共享

image.png
image.png

  1. 自由方法:定义在类命名空间中的普通函数

image.png
image.png
image.png
4. 静态方法:定义在类中的普通方法,但可以被类中的所有实例对象共享
image.png
image.png

  1. #自由方法,静态方法
  2. class DemoClass:
  3. count = 0
  4. def __init__(self,name):
  5. self.name = name
  6. DemoClass.count += 1
  7. @staticmethod
  8. def foo():
  9. DemoClass.count *= 100
  10. return DemoClass.count
  11. dc1 = DemoClass("老王")
  12. print(DemoClass.foo())
  13. print(dc1.foo())
  1. 保留方法:双下划线开始和结束的方法,保留使用

image.png

(5)类的析构函数

image.png

实例1

银行ATM等待时间

Python类的封装

image.png
大概念封装:属性、方法形成类
小概念封装:私有公开对应的私有属性、公开属性、私有方法、公开方法
封装:属性和方法的抽象
属性的抽象:隐藏属性的内在机理
方法的抽象:隐藏方法的内部逻辑

私有属性、公开属性

公开类属性:即类属性
image.png
似有类属性:仅供当前类访问,子类不能访问
image.png

  • 外部不能访问,仅可在类内部被调用

私有实例属性:仅供在当前类内部访问的实例属性,子类亦不能访问
image.png

  • 只能在类内部被方法所访问
  • image.png ```python

    私有类属性、私有实例属性

    class DemoClass: count = 0 def init(self,name): self.name = name DemoClass.count += 1 def getName(self): return self.name @classmethod def getCount(cls): return DemoClass.__count

dc1 = DemoClass(“老王”) dc2 = DemoClass(“小诸葛”)

print(DemoClass.count) #不能通过外部直接访问

print(DemoClass.getCount()) #仅可通过特定函数访问 print(dc1.getName(),dc2.getName())#通过方法调用

print(dc1.name,dc2.name)#通过方法调用,直接返回会报错

print(dc1._DemoClass__name)#转换形式强行访问

  1. <a name="xHaCS"></a>
  2. ### 私有方法、公开方法
  3. 类内部使用的方法<br /> ![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604363103076-49ba7889-696d-4112-85d6-f74168c66fb4.png#align=left&display=inline&height=137&margin=%5Bobject%20Object%5D&name=image.png&originHeight=273&originWidth=816&size=70575&status=done&style=none&width=408)
  4. ```python
  5. #私有方法、公开方法
  6. class DemoClass:
  7. def __init__(self,name):
  8. self.__name = name
  9. def __getName(self):#变成私有方法
  10. if self.__name != "":
  11. return self.__name
  12. else:
  13. return "老张"
  14. def printName(self):
  15. return "{}同志".format(self.__getName())
  16. dc1 = DemoClass("老王")
  17. dc2 = DemoClass("")
  18. print(dc1.printName(),dc2.printName())

保留属性

仅用类名访问:
image.png
保留属性:
image.png
image.png

保留方法

image.png
image.png
image.png image.png
image.png

image.png

Python类的继承

(1)继承

代码复用的高级抽象
实现以类为单位的代码抽象
image.png
只是一些定义的名字
image.png
多继承
image.png

(2)类继承的构建

image.png
image.png
与继承关系判断有关的内置函数
image.png
约束
image.png

(3)python最基础类

image.png
image.png
image.png
基础类有关的内置函数:
image.png
image.png

(4)类的属性重载

image.png
image.png
image.png

(5)类的方法重载

image.png
image.png
image.png

(6)类的多继承

image.png
image.png

  • 深度优先:将第一个类及其父类、超类继承完之后
  • 从左到右:再继承括号内的第二个类,依次从左到右
  • image.png ```python

    函数运行有问题?? — 函数名拼写错误

    类的多继承

    class DemoClass: def init(self,name): self.name = name def printName(self): return self.name

class NameClass: def init(self,title): self.nick = title def printName(self): return self.nick + “同志同志”

class HumanNameClass(NameClass,DemoClass):

class HumanNameClass(DemoClass,NameClass): pass

dc1 = HumanNameClass(“老王”)

dc1 = NameClass(“老张”)

print(dc1.printName())

  1. ![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604363985265-8455d209-a43e-45f7-9e68-abcb3bd6e737.png#align=left&display=inline&height=261&margin=%5Bobject%20Object%5D&name=image.png&originHeight=522&originWidth=1091&size=252634&status=done&style=none&width=545.5)
  2. <a name="aXha0"></a>
  3. ## Python类运算
  4. ![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604367599603-d3a617ce-e702-4ebc-b03a-15791bf2b187.png#align=left&display=inline&height=196&margin=%5Bobject%20Object%5D&name=image.png&originHeight=391&originWidth=912&size=115700&status=done&style=none&width=456)<br />**重载限制**:妥协灵活性与功能性<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604367625118-2906eca5-4479-499b-93a5-0911127cbd63.png#align=left&display=inline&height=104&margin=%5Bobject%20Object%5D&name=image.png&originHeight=208&originWidth=674&size=60929&status=done&style=none&width=337)
  5. <a name="USpcT"></a>
  6. #### 算数运算重载
  7. ![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604367683013-5d609df3-2647-4dcc-930e-b9a09c3d1c85.png#align=left&display=inline&height=106&margin=%5Bobject%20Object%5D&name=image.png&originHeight=212&originWidth=957&size=47647&status=done&style=none&width=478.5)
  8. 一元运算符重载<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604367710516-5b7eb126-01e5-4612-a0b7-eda0ec3b5a40.png#align=left&display=inline&height=153&margin=%5Bobject%20Object%5D&name=image.png&originHeight=306&originWidth=1138&size=224731&status=done&style=none&width=569)<br />二元运算符重载<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604367736119-55e664bf-33f8-4f67-a3d6-ddb7583a5d14.png#align=left&display=inline&height=154&margin=%5Bobject%20Object%5D&name=image.png&originHeight=307&originWidth=1146&size=260969&status=done&style=none&width=573)<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604367751387-0fc33d35-4c80-4989-b420-6ae53b24394c.png#align=left&display=inline&height=132&margin=%5Bobject%20Object%5D&name=image.png&originHeight=263&originWidth=1140&size=220550&status=done&style=none&width=570)<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604367759758-82e15341-0c11-4038-8def-44de5b9ee9a7.png#align=left&display=inline&height=139&margin=%5Bobject%20Object%5D&name=image.png&originHeight=278&originWidth=1137&size=193368&status=done&style=none&width=568.5)<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604367781662-23124092-0414-4e9d-80d6-be0eb23a1787.png#align=left&display=inline&height=136&margin=%5Bobject%20Object%5D&name=image.png&originHeight=271&originWidth=1141&size=206771&status=done&style=none&width=570.5)
  9. <a name="id3oI"></a>
  10. #### 比较运算重载
  11. ![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604394581144-0b0c95c3-51af-40a2-8c85-e277275d4c85.png#align=left&display=inline&height=38&margin=%5Bobject%20Object%5D&name=image.png&originHeight=76&originWidth=721&size=17172&status=done&style=none&width=360.5)<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604394599784-0b98d570-666b-401c-9d93-a882da071ba2.png#align=left&display=inline&height=153&margin=%5Bobject%20Object%5D&name=image.png&originHeight=305&originWidth=1136&size=235535&status=done&style=none&width=568)
  12. <a name="RFN5w"></a>
  13. #### 成员运算重载
  14. ![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604395079276-23bef4aa-21fd-491d-9af7-5e83d7b4d195.png#align=left&display=inline&height=83&margin=%5Bobject%20Object%5D&name=image.png&originHeight=166&originWidth=640&size=37313&status=done&style=none&width=320)<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604395107051-e5bb8f49-9caf-4bc7-805b-de3c4693e895.png#align=left&display=inline&height=152&margin=%5Bobject%20Object%5D&name=image.png&originHeight=304&originWidth=1146&size=271166&status=done&style=none&width=573)<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604395158848-ba7a0132-9982-415d-ac72-67a08a898c28.png#align=left&display=inline&height=74&margin=%5Bobject%20Object%5D&name=image.png&originHeight=147&originWidth=1137&size=88930&status=done&style=none&width=568.5)
  15. <a name="tkS22"></a>
  16. #### 其他运算重载
  17. ![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604395592109-f09a5f68-e072-4429-963d-26d95d33db30.png#align=left&display=inline&height=136&margin=%5Bobject%20Object%5D&name=image.png&originHeight=271&originWidth=1051&size=89583&status=done&style=none&width=525.5)<br /> ![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604395611787-ec9c5cb9-25b6-433e-8cc1-57a35d9458d9.png#align=left&display=inline&height=150&margin=%5Bobject%20Object%5D&name=image.png&originHeight=300&originWidth=1137&size=191075&status=done&style=none&width=568.5)<br /> ![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604395626965-ffe65e54-6ed6-4076-b681-88f5bed5ab97.png#align=left&display=inline&height=151&margin=%5Bobject%20Object%5D&name=image.png&originHeight=301&originWidth=1136&size=191516&status=done&style=none&width=568)<br /> ![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604395638304-6b268c4a-7c8f-44b9-8e83-6bd6ca0b02cb.png#align=left&display=inline&height=69&margin=%5Bobject%20Object%5D&name=image.png&originHeight=137&originWidth=1136&size=101875&status=done&style=none&width=568)
  18. <a name="W86Li"></a>
  19. #### code
  20. ```python
  21. #算术运算符重载
  22. class NewList(list):
  23. #相加重载
  24. def __add__(self,other):
  25. result = []
  26. for i in range(len(self)):
  27. try:
  28. result.append(self[i] + other[i])
  29. except:
  30. result.append(self[i])
  31. return result
  32. #小于比较重载
  33. def __lt__(self,other):
  34. s,t = 0,0
  35. for c in self:
  36. s += c
  37. for c in other:
  38. t += c
  39. return True if s < t else False
  40. #in判断 重载 继承之前特性并且添加和的值是否在其中的判断
  41. def __contains__(self,item):
  42. s = 0
  43. for c in self:
  44. s += c
  45. if super().__contains__(item) or item == s:
  46. return True
  47. else:
  48. return False
  49. #format重载
  50. def __format__(self,format_spec):
  51. t = []
  52. for c in self:
  53. if type(c) == type("字符串"):
  54. t.append(c)
  55. else:
  56. t.append(str(c))
  57. return ", ".join(t)
  58. ls = NewList([6,2,3,4,5,6])
  59. lt = NewList([1,2,3,99])
  60. print(ls + lt)#加号重载 将各对应位置元素相加
  61. print(ls < lt)#小于比较重载 比较列表元素相加之和的大小
  62. ls = NewList([6,1,2,3])
  63. print(6 in ls,12 in ls)#成员判断重载
  64. print(format([6,1,2,3]))
  65. print(format(ls))#格式化重载

Python类多态

参数类型的多态,参数形式的多态
多态的理解:仅针对方法,方法灵活性的抽象
参数类型的多态:一个方法能够处理多个类型
参数形式的多态:一个方法能够接收多个参数的能力
多态是OPP的一个传统概念,Python天生支持多态(弱类型编程语言?),不需要特殊语法
image.pngimage.png
image.png

  1. #多态 参数类型和参数形式的多态
  2. class DemoClass:
  3. def __init__(self,name):
  4. self.name = name
  5. def __id__(self):
  6. return len(self.name)
  7. def lucky(self,salt = 0,more = 9):
  8. s = 0
  9. for c in self.name:
  10. s += (ord(c) + id(salt) + more) % 100
  11. return s
  12. dc1 = DemoClass("老王")
  13. dc2 = DemoClass("老李")
  14. print(dc1.lucky())
  15. print(dc1.lucky(10))
  16. print(dc1.lucky("10"))
  17. print(dc1.lucky(10,100))
  18. print(dc1.lucky(dc2))

实例2 - 图像的四则运算

image.png
image.png
image.png

Python对象的引用

(1)引用的理解

image.png
image.png
image.png

(2)浅拷贝与深拷贝区别

image.png
image.pngimage.png image.png
image.pngimage.png
image.png

(3)类的方法引用

image.png
image.png

Python类的高级话题

image.png

(1)命名空间的理解

image.png
image.png
image.png

(2)类的特征装饰器

image.png
image.png

(3)自定义的异常类型

image.png
image.png

(4)类的名称修饰

image.png
image.png
image.png
image.png
image.png

(5)Python最小空类

image.png
image.png