我没有面向对象编程的经验。我主要用C编程,所以这往往有点令人困惑。基本上我只是想确保我正确地做到了。另外,这是一个继承的工作示例吗?
好的,所以我初始化了一个20x20网格世界并将每个元素设置为None。
class World:
def __init__(self):
self.arr = [[None for x in range(20)] for y in range(20)]
现在我需要在给定x,y坐标的情况下将对象放入指定的元素中。这是世界级的一种方法。
def add_attack_robot(self, team, x, y, direction):
self.arr[x][y] = AttackRobot() <---- **Is this correct?**
这是AttackRobot类:
class AttackRobot(Robot):
def __init__(self,team,x,y,direction):
Robot.__init__(self,team,x,y,direction)
答案 0 :(得分:1)
创建列表列表似乎很合理。在解释器中自己尝试一下(这是关于新Python程序员可以采取的最佳建议):
>>> [[None for x in range(20)] for y in range(20)]
[[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]]
当您尝试实例化AttackRobot
时,您会看到:
TypeError: __init__() takes exactly 5 arguments (1 given)
传递team
,x
,y
和direction
的有效值(self
是隐含的),您应该设置!
E.g:
self.arr[x][y] = AttackRobot(team, x, y, direction)