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'
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()
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()