隐式继承:子类的动作完全等同于父类上的动作
class Parent(object): def implicit(self): print("PARENT implicit()") class Child(Parent): pass dad = Parent() son = Child() dad.implicit() son.implicit()
运行结果:
D:\pytest\venv\Scripts\python.exe D:/pytest/ex44.py PARENT implicit() PARENT implicit()
显式覆盖:子类上的动作完全覆盖了父类上的动作
class Parent(object): def override(self): print("PARENT override()") class Child(Parent): def override(self): print("CHILD override()") dad = Parent() son = Child() dad.override() son.override()
运行结果:
D:\pytest\venv\Scripts\python.exe D:/pytest/ex44.py PARENT override() CHILD override()
son.override()打印出来的是Child.override里的消息,
因为son是Child的一个实例,而子类中新定义的函数在这里取代了父类里的函数
在运行前或运行后替换:子类上的动作部分替换了父类上的动作
class Parent(object): def altered(self): print("PARENT altered()") class Child(Parent): def altered(self): print("CHILD, BEFORE PARENT altered()") super(Child, self).altered() print("CHILD, AFTER PARENT altered()") dad = Parent() son = Child() dad.altered() son.altered()
运行结果:
D:\pytest\venv\Scripts\python.exe D:/pytest/ex44.py PARENT altered() CHILD, BEFORE PARENT altered() PARENT altered() CHILD, AFTER PARENT altered()
重要的是Child中的第9-11行,当调用son.altered()时,玩成了如下操作:
1. 由于覆盖了Parent.altered,实际运行的是Child.altered,所以第9行的运行结果是预料之中的
2. 这里我想在前面和后面加一个动作,所以第9行之后要用super来获取父类的Parent.altered的版本
3. 在第10行,我调用了super(Child, self).altered()方法,这个方法能够意识到继承的发生,并会访问到Parent类
你可以这样读这行代码“用Child和self这两个参数调用super,然后不管它返回什么,调用方法altered ”
4. 在这种情况下,父类的Parent.altered版本执行,并打印出父类Parent里的消息
5. 最后,代码从Parent.altered 返回, Child.altered函数继续打印出剩下的信息
将以上3种继承方式组合使用
# coding=utf-8 class Parent(object): def override(selfs): print("PARENT override()") def implicit(self): print("PARENT implicit") def altered(self): print("PARENT altered()") class Child(Parent): def override(self): print("CHILD override()") def altered(self): print("CHILD,BEFORE PARENT altered()") super(Child, self).altered() print("CHILD, AFTER PARENT altered()") dad = Parent() son = Child() dad.implicit() son.implicit() dad.override() son.override() dad.altered() son.altered()
运行结果:
D:\pytest\venv\Scripts\python.exe D:/pytest/ex44a.py PARENT implicit PARENT implicit PARENT override() CHILD override() PARENT altered() CHILD,BEFORE PARENT altered() PARENT altered() CHILD, AFTER PARENT altered()
组合
# coding=utf-8 class Other(object): def override(self): print("OTHER override()") def implicit(self): print("OTHER implicit()") def altered(self): print("OTHER altered()") class Child(object): def __init__(self): self.other = Other() def implicit(self): self.other.implicit() def override(self): print("CHILD override()") def altered(self): print("CHILD, BEFORE OTHER altered()") self.other.altered() print("CHILD, AFTER OTHER altered()") son = Child() son.implicit() son.override() son.altered()
运行结果:
D:\pytest\venv\Scripts\python.exe D:/pytest/ex44a.py PARENT implicit PARENT implicit PARENT override() CHILD override() PARENT altered() CHILD,BEFORE PARENT altered() PARENT altered() CHILD, AFTER PARENT altered()
组合里没有使用Parent这个名称,因为不是父类子类的关系,而是一个A里有B的关系,这里Child里有一个Other用来玩成它的功能