python 多继承的实现

Posted i勤能补拙

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 多继承的实现相关的知识,希望对你有一定的参考价值。

第一类多继承的实现
from Child import Child
def main():
c = Child(300, 100)
print(c.money, c.faceValue)
c.play()
c.eat()
#注意:父类中方法名相同,默认调用的是在括号中排前面的父类中的方法
c.func()
if __name__ == "__main__":
main()

第二类Mother
class Mother(object):
def __init__(self, faceValue):
self.faceValue = faceValue
def eat(self):
print("eat")
def func(self):
print("func2")

第三类Father
class Father(object):
def __init__(self, money):
self.money = money
def play(self):
print("play")
def func(self):
print("func1")

第四类Child
from Father import Father
from Mother import Mother

class Child(Father, Mother):
def __init__(self, money, faceValue):
#写法
Father.__init__(self, money)
Mother.__init__(self, faceValue)

以上是关于python 多继承的实现的主要内容,如果未能解决你的问题,请参考以下文章

python 多继承

Python 继承

JAVA多线程继承Thread 与实现Runnable接口的相关疑问

Python全栈之路系列----之-----面向对象4接口与抽象,多继承与多态)

Python菱形继承的初始化问题和继承顺序

python 单继承多继承菱形继承