Python——多态

当子类和父类都存在相同的方法时,子类的方法覆盖了父类的方法,在代码运行的时候,总是会调用子类的方法,称之为多态。

#!/usr/bin/python3

class Parent:           # 定义父类
    def myMethod(self):
        print("调用父类方法")

class Child(Parent):    # 定义子类
    def myMethod(self):
       print("调用子类方法")
       
c = Child()             # 子类实例
c.myMethod()            # 子类调用重写方法
super(Child,c).myMethod()       #用子类对象调用父类已被覆盖的方法

执行结果:

调用子类方法
调用父类方法

super() 函数是用于调用父类(超类)的一个方法。

super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种问题。

MRO 就是类的方法解析顺序表, 其实也就是继承父类方法时的顺序表。

语法:

super(type[, object-or-type])

参数

  • type -- 类。
  • object-or-type -- 类,一般是 self

定义一个class的时候,我们实际上就定义了一种数据类型。我们定义的数据类型和 Python 自带的数据类型,比如 str、list、dict 没什么两样:

>>> class Animal(object):
...     def run(self):
...             print('animal is running ...')
...
>>>
>>> class Dog(Animal):
...     def run(self):
...             print('dog is running ...')
...
>>>
>>> a = list()          # list 类型
>>>
>>> b =  Animal()       # Animal 类型
>>>
>>> c = Dog()           # Dog 类型
>>>
>>> isinstance( a, list)
True
>>>
>>> isinstance(b,Animal)
True
>>>
>>> isinstance( c,Dog)
True
>>>
>>> isinstance(c,Animal)
True
>>>
>>> isinstance(b,Dog)
False
>>>

abc确实对应着listAnimalDog这3种类型。

c不仅仅是Dogc还是Animal!因为 Dog 是从 Animal 继承下来的。

在继承关系中,如果一个实例的数据类型是某个子类,那它的数据类型也可以被看做是父类。但是,反过来就不行:

Dog 可以看成 Animal,但 Animal 不可以看成 Dog

接受一个 Animal 类型变量的函数:

>>> def run_twice(animal):
...     animal.run()
...     animal.run()
...
>>>
>>> run_twice(Animal())
animal is running ...
animal is running ...
>>>
>>> run_twice(Dog())
dog is running ...
dog is running ...
>>>
>>> class Cat(Animal):
...     def run(self):
...             print("Cat is running ...")
...
>>>
>>> run_twice(Cat())
Cat is running ...
Cat is running ...

新增一个Animal的子类,不必对 run_twice() 做任何修改,实际上,任何依赖  Animal 作为参数的函数或者方法都可以不加修改地正常运行,原因就在于多态。

多态的好处就是,当我们需要传入DogCat、……时,我们只需要接收  Animal 类型就可以了,因为DogCat ……都是Animal类型,然后,按照Animal类型进行操作即可。由于Animal类型有run()方法,因此,传入的任意类型,只要是Animal类或者子类,就会自动调用实际类型的run()方法,这就是多态的意思:

对于一个变量,我们只需要知道它是Animal类型,无需确切地知道它的子类型,就可以放心地调用run()方法,而具体调用的run()方法是作用在AnimalDog还是Cat对象上,由运行时该对象的确切类型决定,这就是多态真正的威力:调用方只管调用,不管细节,而当我们新增一种Animal的子类时,只要确保run()方法编写正确,不用管原来的代码是如何调用的。这就是著名的“开闭”原则:

对扩展开放:允许新增Animal子类;

对修改封闭:不需要修改依赖Animal类型的run_twice()等函数。

猜你喜欢

转载自blog.csdn.net/qq_41573234/article/details/82083416