python多重继承super父类参数_python – 多重继承如何与super()和不同的__init __()参数一起使用?...

我只是潜入一些更先进的蟒蛇科目(至少先进给我)。我现在正在阅读有关多重继承,以及如何使用super()。我或多或少的了解超级功能的使用方式,但(1)这样做是怎么回事?

class First(object):

def __init__(self):

print "first"

class Second(object):

def __init__(self):

print "second"

class Third(First, Second):

def __init__(self):

First.__init__(self)

Second.__init__(self)

print "that's it"

usage of super() will also be correct when the Derived class inherits

from multiple base classes and some or all of them have init

methods

所以我重写了上面的例子如下:

class First(object):

def __init__(self):

print "first"

class Second(object):

def __init__(self):

print "second"

class Third(First, Second):

def __init__(self):

super(Third, self).__init__(self)

print "that's it"

然而,这只能运行第一个可以找到的init,这是在First中。 (2)可以使用super()来运行来自第一和第二的init,如果是这样,怎么办?运行super(Third,self).__ init __(self)两次只运行First.init()两次..

增加一些混乱。如果继承的类的init()函数采用不同的参数怎么办?例如,如果我有这样的话,该怎么办?

class First(object):

def __init__(self, x):

print "first"

class Second(object):

def __init__(self, y, z):

print "second"

class Third(First, Second):

def __init__(self, x, y, z):

First.__init__(self, x)

Second.__init__(self, y, z)

print "that's it"

(3)如何使用super()将相关参数提供给不同的继承类init函数?

欢迎所有技巧!

PS。由于我有几个问题,我让他们大胆和编号他们..

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值