继承的概念

拓展1:经典类或旧式类

不由任意内置类型派⽣出的类,称之为经典类

  1. class 类名:
  2. 代码
  3. ......

拓展2:新式类

  1. class 类名(object):
  2. 代码

Python⾯向对象的继承指的是多个类之间的所属关系,即⼦类默认继承⽗类的所有属性和⽅法,具体如

下

  1. # ⽗类A
  2. class A(object):
  3. def __init__(self):
  4. self.num = 1
  5. def info_print(self):
  6. print(self.num)
  7. # ⼦类B
  8. class B(A):
  9. pass
  10. result = B()
  11. result.info_print() # 1

在Python中,所有类默认继承object类,object类是顶级类或基类;其他⼦类叫做派⽣类

单继承

故事主线:⼀个煎饼果⼦⽼师傅,在煎饼果⼦界摸爬滚打多年,研发了⼀套精湛的摊煎饼果⼦的

技术。师⽗要把这套技术传授给他的唯⼀的最得意的徒弟

  1. # 1. 师⽗类
  2. class Master(object):
  3. def __init__(self):
  4. self.kongfu = '[古法煎饼果⼦配⽅]'
  5. def make_cake(self):
  6. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  7. # 2. 徒弟类
  8. class Prentice(Master):
  9. pass
  10. # 3. 创建对象daqiu
  11. daqiu = Prentice()
  12. # 4. 对象访问实例属性
  13. print(daqiu.kongfu)
  14. # 5. 对象调⽤实例⽅法
  15. daqiu.make_cake()

多继承

故事推进:daqiu是个爱学习的好孩⼦,想学习更多的煎饼果⼦技术,于是,在百度搜索到⿊⻢程

序员,报班学习煎饼果⼦技术。

所谓多继承意思就是⼀个类同时继承了多个⽗类。

  1. class Master(object):
  2. def __init__(self):
  3. self.kongfu = '[古法煎饼果⼦配⽅]'
  4. def make_cake(self):
  5. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  6. # 创建学校类
  7. class School(object):
  8. def __init__(self):
  9. self.kongfu = '[⿊⻢煎饼果⼦配⽅]'
  10. def make_cake(self):
  11. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  12. class Prentice(School, Master):
  13. pass
  14. daqiu = Prentice()
  15. print(daqiu.kongfu)
  16. daqiu.make_cake()

注意:当⼀个类有多个⽗类的时候,默认使⽤第⼀个⽗类的同名属性和⽅法。

⼦类重写⽗类同名⽅法和属性

故事:daqiu掌握了师⽗和培训的技术后,⾃⼰潜⼼钻研出⾃⼰的独⻔配⽅的⼀套全新的煎饼果⼦

技术

  1. class Master(object):
  2. def __init__(self):
  3. self.kongfu = '[古法煎饼果⼦配⽅]'
  4. def make_cake(self):
  5. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  6. class School(object):
  7. def __init__(self):
  8. self.kongfu = '[⿊⻢煎饼果⼦配⽅]'
  9. def make_cake(self):
  10. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  11. # 独创配⽅
  12. class Prentice(School, Master):
  13. def __init__(self):
  14. self.kongfu = '[独创煎饼果⼦配⽅]'
  15. def make_cake(self):
  16. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  17. daqiu = Prentice()
  18. print(daqiu.kongfu)
  19. daqiu.make_cake()
  20. print(Prentice.__mro__)

⼦类和⽗类具有同名属性和⽅法,默认使⽤⼦类的同名属性和⽅法

⼦类调⽤⽗类的同名⽅法和属性

故事:很多顾客都希望也能吃到古法和⿊⻢的技术的煎饼果⼦。

  1. class Master(object):
  2. def __init__(self):
  3. self.kongfu = '[古法煎饼果⼦配⽅]'
  4. def make_cake(self):
  5. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  6. class School(object):
  7. def __init__(self):
  8. self.kongfu = '[⿊⻢煎饼果⼦配⽅]'
  9. def make_cake(self):
  10. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  11. class Prentice(School, Master):
  12. def __init__(self):
  13. self.kongfu = '[独创煎饼果⼦配⽅]'
  14. def make_cake(self):
  15. # 如果是先调⽤了⽗类的属性和⽅法,⽗类属性会覆盖⼦类属性,故在调⽤属性前,先调⽤
  16. ⾃⼰⼦类的初始化
  17. self.__init__()
  18. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  19. # 调⽤⽗类⽅法,但是为保证调⽤到的也是⽗类的属性,必须在调⽤⽅法前调⽤⽗类的初始化
  20. def make_master_cake(self):
  21. Master.__init__(self)
  22. Master.make_cake(self)
  23. def make_school_cake(self):
  24. School.__init__(self)
  25. School.make_cake(self)
  26. daqiu = Prentice()
  27. daqiu.make_cake()
  28. daqiu.make_master_cake()
  29. daqiu.make_school_cake()
  30. daqiu.make_cake()

多层继承

故事:N年后,daqiu⽼了,想要把所有技术传承给⾃⼰的徒弟

  1. def __init__(self):
  2. self.kongfu = '[古法煎饼果⼦配⽅]'
  3. def make_cake(self):
  4. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  5. class School(object):
  6. def __init__(self):
  7. self.kongfu = '[⿊⻢煎饼果⼦配⽅]'
  8. def make_cake(self):
  9. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  10. class Prentice(School, Master):
  11. def __init__(self):
  12. self.kongfu = '[独创煎饼果⼦配⽅]'
  13. def make_cake(self):
  14. self.__init__()
  15. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  16. def make_master_cake(self):
  17. Master.__init__(self)
  18. Master.make_cake(self)
  19. def make_school_cake(self):
  20. School.__init__(self)
  21. School.make_cake(self)
  22. # 徒孙类
  23. class Tusun(Prentice):
  24. pass
  25. xiaoqiu = Tusun()
  26. xiaoqiu.make_cake()
  27. xiaoqiu.make_school_cake()
  28. xiaoqiu.make_master_cake()

super()调⽤⽗类⽅法

  1. class Master(object):
  2. def __init__(self):
  3. self.kongfu = '[古法煎饼果⼦配⽅]'
  4. def make_cake(self):
  5. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  6. class School(Master):
  7. def __init__(self):
  8. self.kongfu = '[⿊⻢煎饼果⼦配⽅]'
  9. def make_cake(self):
  10. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  11. # ⽅法2.1
  12. # super(School, self).__init__()
  13. # super(School, self).make_cake()
  14. # ⽅法2.2
  15. super().__init__()
  16. super().make_cake()
  17. def __init__(self):
  18. self.kongfu = '[独创煎饼果⼦技术]'
  19. def make_cake(self):
  20. self.__init__()
  21. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  22. # ⼦类调⽤⽗类的同名⽅法和属性:把⽗类的同名属性和⽅法再次封装
  23. def make_master_cake(self):
  24. Master.__init__(self)
  25. Master.make_cake(self)
  26. def make_school_cake(self):
  27. School.__init__(self)
  28. School.make_cake(self)
  29. # ⼀次性调⽤⽗类的同名属性和⽅法
  30. def make_old_cake(self):
  31. # ⽅法⼀:代码冗余;⽗类类名如果变化,这⾥代码需要频繁修改
  32. # Master.__init__(self)
  33. # Master.make_cake(self)
  34. # School.__init__(self)
  35. # School.make_cake(self)
  36. # ⽅法⼆: super()
  37. # ⽅法2.1 super(当前类名, self).函数()
  38. # super(Prentice, self).__init__()
  39. # super(Prentice, self).make_cake()
  40. # ⽅法2.2 super().函数()
  41. super().__init__()
  42. super().make_cake()
  43. daqiu = Prentice()
  44. daqiu.make_old_cake()

注意:使⽤super() 可以⾃动查找⽗类。调⽤顺序遵循 mro 类属性的顺序。⽐较适合单继承使⽤

私有权限

定义私有属性和⽅法

在Python中,可以为实例属性和⽅法设置私有权限,即设置某个实例属性或实例⽅法不继承给⼦类

设置私有权限的⽅法:在属性名和⽅法名 前⾯ 加上两个下划线 __。

故事:daqiu把技术传承给徒弟的同时,不想把⾃⼰的钱(2000000个亿)继承给徒弟,这个时候就

要为 钱 这个实例属性设置私有权限

  1. class Master(object):
  2. def __init__(self):
  3. self.kongfu = '[古法煎饼果⼦配⽅]'
  4. def make_cake(self):
  5. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  6. class School(object):
  7. def __init__(self):
  8. self.kongfu = '[⿊⻢煎饼果⼦配⽅]'
  9. def make_cake(self):
  10. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  11. class Prentice(School, Master):
  12. def __init__(self):
  13. self.kongfu = '[独创煎饼果⼦配⽅]'
  14. # 定义私有属性
  15. self.__money = 2000000
  16. # 定义私有⽅法
  17. def __info_print(self):
  18. print(self.kongfu)
  19. print(self.__money)
  20. def make_cake(self):
  21. self.__init__()
  22. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  23. def make_master_cake(self):
  24. Master.__init__(self)
  25. Master.make_cake(self)
  26. def make_school_cake(self):
  27. School.__init__(self)
  28. School.make_cake(self)
  29. # 徒孙类
  30. class Tusun(Prentice):
  31. pass
  32. daqiu = Prentice()
  33. # 对象不能访问私有属性和私有⽅法
  34. # print(daqiu.__money)
  35. # daqiu.__info_print()
  36. xiaoqiu = Tusun()
  37. # ⼦类⽆法继承⽗类的私有属性和私有⽅法
  38. # print(xiaoqiu.__money) # ⽆法访问实例属性__money
  39. # xiaoqiu.__info_print()

注意:私有属性和私有⽅法只能在类⾥⾯访问和修改。

获取和修改私有属性值

在Python中,⼀般定义函数名 get_xx ⽤来获取私有属性,定义 set_xx ⽤来修改私有属性值。

  1. class Master(object):
  2. def __init__(self):
  3. self.kongfu = '[古法煎饼果⼦配⽅]'
  4. def make_cake(self):
  5. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  6. class School(object):
  7. def __init__(self):
  8. self.kongfu = '[⿊⻢煎饼果⼦配⽅]'
  9. def make_cake(self):
  10. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  11. class Prentice(School, Master):
  12. def __init__(self):
  13. self.kongfu = '[独创煎饼果⼦配⽅]'
  14. self.__money = 2000000
  15. # 获取私有属性
  16. def get_money(self):
  17. return self.__money
  18. # 修改私有属性
  19. def set_money(self):
  20. self.__money = 500
  21. def __info_print(self):
  22. print(self.kongfu)
  23. print(self.__money)
  24. def make_cake(self):
  25. self.__init__()
  26. print(f'运⽤{self.kongfu}制作煎饼果⼦')
  27. def make_master_cake(self):
  28. Master.__init__(self)
  29. Master.make_cake(self)
  30. def make_school_cake(self):
  31. School.__init__(self)
  32. School.make_cake(self)
  33. # 徒孙类
  34. class Tusun(Prentice):
  35. pass
  36. daqiu = Prentice()
  37. xiaoqiu = Tusun()
  38. # 调⽤get_money函数获取私有属性money的值
  39. print(xiaoqiu.get_money())
  40. # 调⽤set_money函数修改私有属性money的值
  41. xiaoqiu.set_money()
  42. print(xiaoqiu.get_money())