您的位置:首页 > 编程语言 > Python开发

python普通继承和super继承

2016-04-05 14:52 651 查看
普通继承:

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 !!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 继承 super