一、多重继承

Python支持多重继承,一个子类可以有多个“直接父类”。这样,就具备了“多个父类”的特点。但是由于,这样会被“类的整体层次”搞的异常复杂,尽量避免使用。
class A:
  def aa(self):
    print("aa")
class B:
  def bb(self):
    print("bb")
class C(B,A):
  def cc(self):
    print("cc")
c = C()
c.cc()
c.bb()
c.aa()
类结构为:

二、MRO方法解析顺序
Python支持多继承,如果父类中有相同名字的方法,在子类没有指定父类名时,解释器将“从左向右”按顺序搜索。
MRO(Method Resolution Order):方法解析顺序。 我们可以通过mro()方法获得“类的层次结构”,方法解析顺序也是按照这个“类的层次结构”寻找的。
class A:
  def aa(self):
    print("aa")
  def say(self):
    print("say AAA!")
class B:
  def bb(self):
    print("bb")
  def say(self):
    print("say BBB!")
class C(B,A):
  def cc(self):
    print("cc")
c = C()
print(C.mro())     #打印类的层次结构
c.say()         #解释器寻找方法是“从左到右”的方式寻找,此时会执行B类中的say()
执行结果:
[main.C'>, main.B'>, main.A'>, ] say BBB!
三、super()获得父类定义
在子类中,如果想要获得父类的方法时,我们可以通过super()来做。
super()代表父类的定义,不是父类对象。
❤️想调用父类的构造方法:
super(子类名称,self).__init__(参数列表)
class A:
  def __init__(self):
    print("A的构造方法")
  def say(self):
    print("A: ",self)
    print("say AAA")
class B(A):
  def __init__(self):
    super(B,self).__init__() #调用父类的构造方法
    print("B的构造方法")
  def say(self):
    #A.say(self)   调用父类的say方法
    super().say()  #通过super()调用父类的方法
    print("say BBB")
b = B()
b.say()
运行结果:
A: <__main__.B object at 0x007A5690>
say AAA
say BBB
四、多态

多态(polymorphism)是指同一个方法调用由于对象不同可能会产生不同的行为。
比如:现实生活中,同一个方法,具体实现会完全不同。 比如:同样是调用人“吃饭”的方法,中国人用筷子吃饭,英国人用刀叉吃饭,印度人用手吃饭。
关于多态要注意以下2点:
- 多态是方法的多态,属性没有多态。
- 多态的存在有2个必要条件:继承、方法重写
#多态
class Animal:
  def shout(self):
    print("动物叫了一声")
class Dog(Animal):
  def shout(self):
    print("小狗,汪汪汪")
class Cat(Animal):
  def shout(self):
    print("小猫,喵喵喵")
def animalShout(a):
  a.shout()  #传入的对象不同,shout方法对应的实际行为也不同。
animalShout(Dog())
animalShout(Cat())
五、特殊方法和运算符重载
Python的运算符实际上是通过调用对象的特殊方法实现的。
a = 20
b = 30
c = a+b
d = a.__add__(b)
print("c=",c)
print("d=",d)
运算结果:
c= 50
d= 50
常见的特殊方法统计如下:
| 方法 | 说明 | 例子 | 
|---|---|---|
| __init__ | 构造方法 | 对象创建和初始化: p = Person() | 
| __del__ | 析构方法 | 对象回收 | 
| __repr__,__str__ | 打印,转换 | print(a) | 
| __call__ | 函数调用 | a() | 
| __getattr__ | 点号运算 | a.xxx | 
| __setattr__ | 属性赋值 | a.xxx = value | 
| __getitem__ | 索引运算 | a[key] | 
| __setitem__ | 索引赋值 | a[key]=value | 
| __len__ | 长度 | len(a) | 
每个运算符实际上都对应了相应的方法,统计如下:
| 运算符 | 特殊方法 | 说明 | 
|---|---|---|
| + | __add__ | 加法 | 
| - | __sub__ | 减法 | 
| <<=== | __lt____le____eq__ | 比较运算符 | 
| >>=!= | __gt____ge____ne__ | 比较运算符 | 
| |^& | __or____xor____and__ | 或、异或、与 | 
| <<>> | __lshift____rshift__ | 左移、右移 | 
| */%// | __mul____truediv____mod____floordiv__ | 乘、浮点除、模运算(取余)、整数除 | 
| ** | __pow__ | 指数运算 | 
我们可以重写上面的特殊方法,即实现了“运算符的重载”。
#测试运算符的重载
class Person:
  def __init__(self,name):
    self.name = name
  def __add__(self, other):
    if isinstance(other,Person):
      return "{0}--{1}".format(self.name,other.name)
    else:
      return "不是同类对象,不能相加"
  def __mul__(self, other):
    if isinstance(other,int):
      return self.name*other
    else:
      return "不是同类对象,不能相乘"
p1 = Person("高淇")
p2 = Person("高希希")
x = p1 + p2
print(x)
print(p1*3)
运算结果:
高淇--高希希
高淇高淇高淇



















