python的super关键字使用示例,pythonsuper关键字,super在单继承中使用


super在单继承中使用的例子:

class Foo():    def __init__(self, frob, frotz)        self.frobnicate = frob        self.frotz = frotzclass Bar(Foo):    def __init__(self, frob, frizzle)        super().__init__(frob, 34)        self.frazzle = frizzle

此例子适合python 3.x,更多请参考super

如果要在python2.x下使用则需要稍作调整,如下代码示例:

class Foo(object):     def __init__(self, frob, frotz):         self.frobnicate = frob         self.frotz = frotz class Bar(Foo):     def __init__(self, frob, frizzle):         super(Bar,self).__init__(frob,34)         self.frazzle = frizzle new = Bar("hello","world") print new.frobnicate print new.frazzle print new.frotz 

评论关闭