Python class的继承方法代码实例
更新时间:2020年02月14日 09:28:22 作者:conpi
这篇文章主要介绍了Python class的继承方法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
这篇文章主要介绍了Python class的继承方法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
class parent(object):
def implicit(self):
print("Parent implicit()")
def override(self):
print("Parent override()")
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()
运行结果:
Parent implicit() Parent implicit() Parent override() Child override() Parent altered() Child,Before Parent altered() Parent altered() Child,After Parent altered()
还可以写成:
class parent():
def implicit(self):
print("Parent implicit()")
def override(self):
print("Parent override()")
def altered(self):
print("Parent altered()")
class child(parent):
def __init__(self):
self.parent =parent()
def implicit(self):
self.parent.implicit()
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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Python使用matplotlib给柱状图添加数据标签bar_label()
这篇文章主要介绍了Python使用matplotlib给柱状图添加数据标签bar_label(),记录如何用使用matplotlib给柱状图添加数据标签,是以matplotlib.pyplot.bar_label()为例,需要的朋友可以参考一下2022-03-03


最新评论