sto risolvendo questo problema:Classe eredità in pitone
Si consideri il seguente gerarchia di classi:
class Person(object): def __init__(self, name): self.name = name def say(self, stuff): return self.name + ' says: ' + stuff def __str__(self): return self.name class Lecturer(Person): def lecture(self, stuff): return 'I believe that ' + Person.say(self, stuff) class Professor(Lecturer): def say(self, stuff): return self.name + ' says: ' + self.lecture(stuff) class ArrogantProfessor(Professor): def say(self, stuff): return 'It is obvious that ' + self.say(stuff)
Come scritto, questo codice porta ad un ciclo infinito quando si utilizza la Arrogante classe Professore .
cambiare la definizione di ArrogantProfessor in modo che il comportamento seguente si ottiene:
e = Person('eric') le = Lecturer('eric') pe = Professor('eric') ae = ArrogantProfessor('eric') e.say('the sky is blue') #returns eric says: the sky is blue le.say('the sky is blue') #returns eric says: the sky is blue le.lecture('the sky is blue') #returns believe that eric says: the sky is blue pe.say('the sky is blue') #returns eric says: I believe that eric says: the sky is blue pe.lecture('the sky is blue') #returns believe that eric says: the sky is blue ae.say('the sky is blue') #returns eric says: It is obvious that eric says: the sky is blue ae.lecture('the sky is blue') #returns It is obvious that eric says: the sky is blue
La mia soluzione a questo è:
class ArrogantProfessor(Person):
def say(self, stuff):
return Person.say(self, ' It is obvious that ') + Person.say(self,stuff)
def lecture(self, stuff):
return 'It is obvious that ' + Person.say(self, stuff)
Ma il correttore dà solo marchi mezzo per questa soluzione . Qual è l'errore che sto facendo e quali sono i casi di test in cui questo codice fallisce? (Sono nuovo di pitone e imparato a conoscere le classi di qualche tempo fa.)
è che un errore di battitura nella soluzione per ' le.lecture ('il cielo è blu') ', o manca davvero il pronome" I "? – L3viathan
@ L3viathan che era un errore di battitura –