2016-03-12 16 views
12

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

+0

è che un errore di battitura nella soluzione per ' le.lecture ('il cielo è blu') ', o manca davvero il pronome" I "? – L3viathan

+0

@ L3viathan che era un errore di battitura –

risposta

7

Probabilmente dovrebbe usare super() al posto di hard-cablaggio della classe 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

Come ex selezionatore di codifica hw, presumo , dovresti aver prodotto l'output desiderato senza rendere ArrogantProfessor un semplice Person. Dopo tutto, il nome della classe indica che dovrebbe ancora sottoclasse Professor.

2

Probabilmente vuole che tu riceva effettivamente la classe genitore. Il modo per farlo è semplice.

python2/3:

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

Python 3 solo:

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

In entrambi i casi, ae.say("something") deve restituire:

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

Questo è perché la classe genitore è Professor, non Person.

Allo stesso modo, nella classe lezione, si dovrebbe fare:

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

Non è proprio chiaro che cosa è che si vuole, però.

4

E 'stato dato che:

class ArrogantProfessor(Professor): 

ma avete fatto queste cose:

class ArrogantProfessor(Person): 

che ha portato nel grado dimezzato.

+0

In realtà ho usato per la prima volta il Professor come argomento ma non ha funzionato, quindi l'ho cambiato in Persona –

+1

Questo era l'obiettivo dell'assunzione @johnsmith! Per farti * pensare * come farlo funzionare con 'Professore'. Bella domanda, hai il mio upvote. – gsamaras

2

Questo dovrebbe essere:

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

Non è necessario definire say() in ArrogantProfessor, perché è già definito in Professor, e utilizzerà il metodo lecture() definito nella classe del bambino.

2

È un po 'difficile da dire senza sapere cosa stanno cercando di insegnarti. Una probabile ipotesi è che sei stato insegnato eredità, e se ne sono andati oltre super è probabile che li desiderano utilizzarlo per avere un'occhiata uscita del ArrogantProfessor come:

eric says: It is obvious that STUFF 

Dove roba è la stringa . 're passando

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

Dovresti avere il contesto per la tua risposta, non solo il codice. – Jeff

0

per la seconda parte, la risposta corretta è:

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

per la prima parte del codice è:

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

per la seconda parte del codice è:

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

per la terza parte del codice è:

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

spero che sia utile