
近日,在阅读源代码遇到父类使用子类的方法属性,举个简单的例子:
class parent(object):
def test(self):
self.child_attribute # 子类的属性?
print self # <__main__.child object at 0x100597050>
self.child_method()
class child(parent):
def __init__(self):
self.child_attribute = 'child'
def child_method(self):
print 'child method is called'
c=child()
c.test()
print dir(c)
实例c 确实有test方法,self用指向child实例,
总之,父类及子类之间的方法及属性是共享的。




近期评论