class Point(object):
def __init__(self, x=0, y=0):
self.__x = x
self.__y = y
print 'point ({x},{y}) created.'.format( x=self.__x, y=self.__y )
class Line(object):
def __init__(self, start_point=Point(), end_point=Point()):
print 'Line.__init__ called.'
self.start = start_point
self.end = end_point
def test_line():
p1 = Point(1,1)
p2 = Point(2,2)
line1 = Line(p1, p2)
if __name__=='__main__':
test_line()
当我运行这个脚本时,它给出了结果:
point (0,0) created.
point (0,0) created.
point (1,1) created.
point (2,2) created.
Line.__init__ called.
我不知道为什么在创建 line1 之前调用 Point.__init() 。