首页 / 知识

python支持多继承吗

2023-11-12 13:15:00

python多继承

单继承有时候可能满足不了我们所需的所以我们就会遇到多继承,这个同样能够展示出代码的重用。

同样是上边的例子,child不仅仅是继承来自父亲,还继承来自母亲。所以我们创建mother类

classMother(object):

def__init__(self,face):

self.face=face

print('face',face)

defplay(self):

print('mothergoshoppingwithme')

mothe类创建的属性为face,其次我们还定义的一个相同的方法play是为了展示多继承中如果有相同的函数会调用哪个。

然后我们重写一下child类

fromFatherimportFather

fromMotherimportMother

classChild(Mother,Father):

def__init__(self,money,face):

Father.__init__(self,money)

Mother.__init__(self,face)

以上内容为大家介绍了python培训之支持多继承吗,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注我们

培训不了代码上边函数

最新内容

相关内容

热门文章

推荐文章

标签云

猜你喜欢