2016-03-12 10 views
12

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.)

+0

Ist ist ' le.lecture ("Der Himmel ist blau"), oder fehlt wirklich das Pronomen "Ich"? – L3viathan

+0

@ L3viathan das war ein Tippfehler –

Antwort

7

Sie sollten wahrscheinlich super() anstelle von Hart Verdrahtung der Klasse Person:

class ArrogantProfessor(Person): 
    def say(self, stuff): 
     return super(ArrogantProfessor, self).say(self.lecture(stuff)) 
    def lecture(self, stuff): 
     return 'It is obvious that ' + super(ArrogantProfessor, self).say(stuff) 
2

Als ehemaliger grader von hw Codierung, nehme ich an , sollten Sie die gewünschte Ausgabe produziert haben, ohne ArrogantProfessor eine bloße Person zu machen. Immerhin gibt der Klassenname an, dass es immer noch Unterklasse Professor sein sollte.

2

Wahrscheinlich möchte er, dass Sie tatsächlich die Elternklasse bekommen. Der Weg dahin ist einfach.

Python2/3:

class ArrogantProfessor(Professor): 
    def say(self, stuff): 
     return 'It is obvious that ' + super(ArrogantProfessor, self).say(stuff) 

Python 3 nur:

class ArrogantProfessor(Professor): 
    def say(self, stuff): 
     return 'It is obvious that ' + super().say(stuff) 

In jedem Fall ae.say("something") zurückkehren sollte:

"It is obvious that eric says: I believe that eric says: something" 

Dies liegt daran, die übergeordnete Klasse Professor ist, nicht Person.

Auch in Ihrem Vortrag Klasse, sollten Sie tun:

def lecture(self, stuff): 
    return 'I believe that ' + super(Lecturer, self).say(self, stuff) # or the Python3 version if you're using that 

Es ist nicht wirklich klar, was es ist, dass Sie wollen, though.

4

war es da:

class ArrogantProfessor(Professor): 

, aber sie taten dies:

class ArrogantProfessor(Person): 

, die in der halbierten Klasse geführt.

+0

Eigentlich habe ich zuerst Professor als Argument verwendet, aber das hat nicht funktioniert, also änderte ich das zu Person –

+1

Das war das Ziel der Zuweisung @johnsmith! Um dich * denken * wie man es mit 'Professor' arbeiten lässt. Gute Frage, du hast meine Erwiderung bekommen. – gsamaras

2

sollte sein:

class ArrogantProfessor(Professor): 
    def lecture(self, stuff): 
     return 'It is obvious that ' + Person.say(self,stuff) 

Sie müssen in der untergeordneten Klasse nicht definieren say() in ArrogantProfessor, weil sie bereits in Professor definiert ist, und es wird verwenden, um die lecture() Methode definiert.

2

Es ist ein bisschen schwer zu sagen, ohne zu wissen, was sie dir beibringen wollen. Eine wahrscheinliche Vermutung ist, dass Sie die Vererbung gelehrt wir Sie, und wenn sie über super gegangen sind, es ist wahrscheinlich, dass sie wollen, dass es zu verwenden, wie die ArrogantProfessor den Ausgang Blick zu haben:

eric says: It is obvious that STUFF 

Wo STUFF ist die Zeichenfolge . ist nebenbei

0
 class Professor(Lecturer): 
     def say(self, stuff): 
      return "Prof. " + self.name + ' says: ' + self.lecture(stuff) 

    class ArrogantProfessor(Professor): 
     def lecture(self, stuff):   
      return 'It is obvious that I believe that ' + Person.say(self, stuff) 
+0

Sie sollten Kontext für Ihre Antwort haben, nicht nur Code. – Jeff

0

für den zweiten Teil, die richtige Antwort ist:

class ArrogantProfessor(Professor): 
    def lecture(self, stuff): 
     return 'It is obvious that ' + Lecturer.lecture(self,stuff) 
0

für Teil eins der Code:

class ArrogantProfessor(Professor): 
def lecture(self, stuff): 
    return 'It is obvious that ' + Person.say(self,stuff) 

für den zweiten Teil des Codes ist:

class ArrogantProfessor(Professor): 
def lecture(self, stuff): 
    return 'It is obvious that I believe that ' + Person.say(self,stuff) 

für Teil drei der Code:

class Professor(Lecturer): 
def say(self, stuff): 
    return 'Prof. ' + self.name + ' says: ' + self.lecture(stuff) 

Hoffe, dass es für einen Tippfehler in der Lösung nützlich

Verwandte Themen