Ich bin die Lösung dieses Problems:Klassenvererbung in Python
die folgende Hierarchie von Klassen vor:
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)
Wie geschrieben, dieser Code zu einer Endlosschleife führt, wenn die Arrogant Professor-Klasse .
die Definition von ArrogantProfessor ändern, so dass die folgende Verhalten erreicht:
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
Meine Lösung ist:
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)
Aber der Checker gibt nur halbe Noten für diese Lösung . Was ist der Fehler, den ich mache und was sind die Testfälle, bei denen dieser Code versagt? (Ich bin neu in Python und lernte vor einiger Zeit über Klassen.)
Ist ist ' le.lecture ("Der Himmel ist blau"), oder fehlt wirklich das Pronomen "Ich"? – L3viathan
@ L3viathan das war ein Tippfehler –