class first:
def __init__(self):
self.a = '这里是父类方法1'
def action(self):
print('这里是父类方法2')
class second(first):
def __init__(self):
super().__init__()
self.b = '这里是父类方法3'
def action(self):
print('这里是父类方法4')
class thirdly(second):
def __init__(self):
super().__init__()
self.c = "这是子类方法1"
# 重写父类的值 (重新定义了父类原有的值)
self.a = "这里是重写父类的值"
# 使用父类的值
self.d = "%s" % self.b
def NowValues(self):
# 打印父类的值
print(self.b)
class fourthly(thirdly):
def __init__(self):
super().__init__()
self.b = '这是子类方法2'
# 重写父类的值 (重新定义了父类原有的值)
self.a = "这里是重写父类的值1"
def Nowvalues(self):
print('这是子类方法3')
t = fourthly() # 子类thirdly继承父类first的所有属性和方法
t.action() # 方法名称相同,等于重写了父类的内容
t.NowValues() # 子类thirdly进行执行
t.Nowvalues() # 子类fourthly的执行
print(t.a) # 打印重写的值
print(t.b) # 打印重写的值
print(t.c) # 打印重写的值
print(t.d) # 打印重写的值
print(fourthly.__mro__) # 查看执行顺序
执行结果: