2

这在我第一次调用该函数时有效,但第二次我收到错误消息

mloc = pygame.mouse.get_pos()
if type == "gun":
    mTowers.add(gun(mloc))

...

 class gun(tower):    
     def __init__(self, place):
         tower.__init__(self, place)

和错误:

TypeError: __init__() takes exactly 2 arguments (3 given)

我认为我正在做的是将鼠标位置作为元组(和 self 参数)传递。显然,它适用于第一次通话。有什么想法会出错吗?

4

1 回答 1

2

是隐式传递的self,所以你实际上是分别传递了塔、枪(即自我)和地点。

此外,您应该在这里使用超级函数,而不是显式调用tower.__init__

使用 super 的示例(python 2.7):

class Animal(object):
  def __init__(self, nlegs=4):
    print 'in __init__ Animal'
    self.nlegs = nlegs

class Cat(Animal):
  def __init__(self, talk='meow'):
    print 'in __init__ Cat'
    super(Cat, self).__init__()
    self.talk = talk

tom = Cat()
print "I'm a cat with {} legs and I say '{}'".format(tom.nlegs, tom.talk)
于 2012-05-09T02:21:59.707 回答