Oppure

Loading
07/02/15 19:54
LukeMilan
class Tank():
    def __init__(self):
        self.x = 0
        self.y = 0
	def avanti(self):
		self.x+=50

class Player(Tank):
	def start(self):
		Tank.avanti()

tank=Player()
tank.start()


Errore:
Traceback (most recent call last):
  File "test.py", line 13, in <module>
    tank.start()
  File "test.py", line 10, in start
    Tank.avanti()
AttributeError: class Tank has no attribute 'avanti'


Dove e perché ho sbagliato? Grazie mille! :k:
aaa
07/02/15 21:01
dmr
Non uso python da un bel pò(purtroppo), prova ad indentare bene il metodo avanti di Tant al livello del costruttore.
Ultima modifica effettuata da dmr 07/02/15 21:02
aaa
08/02/15 12:59
Poggi Marco
Ciao!

Nel sorgente ci sono due errori; uno di indentazione ( la funzione avanti è interna al costruttore ).
L' altro sta nel richiamo del metodo avanti; va invocato tramite self.

Quindi, il sorgente corretto diventa:
class Tank:
     def __init__(self):
          """Costruttore"""
          self.x=0
          self.y=0
          
     def avanti(self):
          """Avanzamento"""
          self.x+=50

class Player(Tank):
     def start(self):
          """Partenza"""
          self.avanti()

tank=Player()
tank.start()
aaa
09/02/15 14:18
LukeMilan
Postato originariamente da Poggi Marco:

Ciao!

Nel sorgente ci sono due errori; uno di indentazione ( la funzione avanti è interna al costruttore ).
L' altro sta nel richiamo del metodo avanti; va invocato tramite self.

Quindi, il sorgente corretto diventa:
class Tank:
     def __init__(self):
          """Costruttore"""
          self.x=0
          self.y=0
          
     def avanti(self):
          """Avanzamento"""
          self.x+=50

class Player(Tank):
     def start(self):
          """Partenza"""
          self.avanti()

tank=Player()
tank.start()


Grazie mille!
aaa