1. 程式人生 > >python普通繼承和super繼承

python普通繼承和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):         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 !!!!