python普通继承和super继承

来源:互联网 发布:如何安装开票软件 编辑:程序博客网 时间:2024/05/21 06:53
普通继承:
class FooParent(object):
    def __init__(self):
        self.parent='I\'m the parent.'
        print 'Parent'
        
    def bar(self, message):
        print message, 'from Parent'

class FooChild(FooParent):
    def __init__(self):
        FooParent.__init__(self)
        print 'Child'
        
    def bar(self, message):
        FooParent.bar(self, message)
        print 'Child bar function. '

        print self.parent


super继承:

class FooParent(object):
    def __init__(self):
        self.parent = 'I\'m the parent.'
        print 'Parent'
        
    def bar(self, message):
        print message, 'from Parent'

class FooChild(FooParent):
    def __init__(self):
        super(FooChild, self).__init__()
        print 'Child'
        
    def bar(self, message):
        super(FooChild, self).bar(message)
        print 'Child bar function. '
        print self.parent

父类一定要继承object !!!!

0 0
原创粉丝点击