python普通繼承和super繼承
阿新 • • 發佈:2019-01-23
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. '
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
print self.parent
父類一定要繼承object !!!!