2016-03-12 26 views
12

Tôi đang giải quyết vấn đề này:Lớp kế thừa trong python

Hãy xem xét các hệ thống phân cấp sau các lớp:

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) 

Theo văn bản, mã này dẫn đến một vòng lặp vô hạn khi sử dụng lớp Giáo sư Arrogant .

Thay đổi định nghĩa của ArrogantProfessor để các hành vi sau được thực hiện:

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 

Giải pháp của tôi cho điều này là:

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) 

Nhưng kiểm tra cho chỉ dấu nửa cho giải pháp này . Sai lầm mà tôi đang mắc phải là gì và các trường hợp thử nghiệm mà mã này không thành công? (Tôi mới vào python và tìm hiểu về các lớp học một số thời gian trước đây.)

+0

Có phải đó là một lỗi đánh máy trong các giải pháp cho ' le.lecture ('bầu trời xanh') ', hay thực sự đại từ" tôi "bị thiếu? – L3viathan

+0

@ L3viathan đó là lỗi đánh máy –

Trả lời

7

Bạn có lẽ nên sử dụng super() thay vì cứng dây lớp 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

Là một học sinh lớp cũ của mã hóa hw, tôi giả , bạn nên tạo ra kết quả mong muốn mà không cần làm cho ArrogantProfessor chỉ là Person. Sau khi tất cả, tên lớp chỉ ra rằng nó vẫn nên phân lớp Professor.

2

Có thể anh ấy muốn bạn thực sự có được lớp cha. Cách để làm điều này rất đơn giản.

python2/3:

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

Python 3 chỉ:

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

Trong cả hai trường hợp, ae.say("something") nên quay lại:

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

Điều này là do tầng lớp phụ huynh là Professor, không phải Person.

Tương tự như vậy, trong lớp giảng của bạn, bạn nên làm:

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

Nó không thực sự rõ ràng những gì nó là bạn muốn, mặc dù.

4

Nó đã được đưa ra rằng:

class ArrogantProfessor(Professor): 

nhưng bạn đã làm điều này:

class ArrogantProfessor(Person): 

đó dẫn đến việc cấp giảm đi một nửa.

+0

Thực ra lần đầu tiên tôi sử dụng Giáo sư làm đối số nhưng điều đó không có tác dụng nên tôi đã thay đổi điều đó thành Người –

+1

Đó là mục tiêu của @johnsmith assigment! Để làm cho bạn * suy nghĩ * cách làm cho nó hoạt động với 'Giáo sư'. Tốt câu hỏi btw, bạn có upvote của tôi. – gsamaras

2

này nên là:

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

Bạn không cần phải xác định say() trong ArrogantProfessor, bởi vì nó đã được xác định trong Professor, và nó sẽ sử dụng các phương pháp lecture() định nghĩa trong lớp trẻ.

2

Hơi khó nói mà không biết họ đang cố dạy bạn điều gì. Một đoán khả năng là bạn đang bị dạy thừa kế, và nếu họ đã đi qua super nó có khả năng mà họ muốn bạn sử dụng nó để có cái nhìn đầu ra của ArrogantProfessor như:

eric says: It is obvious that STUFF 

đâu STUFF là chuỗi bạn . đang đi qua trong

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

Bạn nên có bối cảnh cho câu trả lời của bạn, không chỉ là mã. – Jeff

0

đối với phần thứ hai, câu trả lời đúng là:

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

cho phần một mã là:

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

cho phần hai mã là:

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

Về phần ba mã là:

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

Hy vọng nó sẽ rất hữu ích