通过__add __

时间:2015-11-19 20:41:50

标签: python class concatenation

我有一个名为“Car”的类,它通过命令 forward 来模拟汽车的运动,并且由于我已经写过的其他一些(默认方向:右)而改变了方向。

E.g. [if starting from (0,0) (top left)]: 
c = Car()
c.forward(8).right(3)  increases the value of the "x" at first: (8, 0) an then changes direction to left (= from "right" the direction becomes "down") and exectues a forward of 3 (= the value of the "y" increased by 3 (8, 3).

我需要做的是在同一个类中修改特殊方法“ add ”来连接两个命令,以使第二个命令从第一个停止并返回新的“Car”对象(包含连接)。

E.g:

  c1 = Car().right(3).left(3)
  c2 = Car().left(3).right(3)
  c = c1 + c2

Should (starting from (2,2)): 
1.0)Change direction to right, execute a forward by "3" -> (2,5)
1.1)Change the direction to left, execute a forward by "3" -> (5,5)
From where the c1 object stopped:
2.0)Change direction to left, execute the forward -> (5,2)
2.1)Change direction to right, execute the last forward (8,2)

我怎么能这样做?

0 个答案:

没有答案