[英]Class inheritance in python
我正在解决这个问题:
考虑以下类的层次结构:
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)
如上所述,当使用Arrogant Professor类时,此代码会导致无限循环。
更改ArrogantProfessor的定义,以便实现以下行为:
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
我的解决方案是:
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)
但是检查器只给出了这个解决方案的一半标记。 我犯的错误是什么,以及此代码失败的测试用例是什么? (我是python的新手,不久前就开始学习类。)
你可能应该使用super()
而不是硬连接类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)
有人说:
class ArrogantProfessor(Professor):
但是你这样做了:
class ArrogantProfessor(Person):
导致成绩减半。
作为一名编码hw的前评级者,我认为,你应该产生所需的输出而不使ArrogantProfessor
成为一个Person
。 毕竟,类名表明它仍然应该是Professor
子类。
他可能希望你真正得到父类。 这样做的方法很简单。
Python2 / 3:
class ArrogantProfessor(Professor):
def say(self, stuff):
return 'It is obvious that ' + super(ArrogantProfessor, self).say(stuff)
仅限Python 3:
class ArrogantProfessor(Professor):
def say(self, stuff):
return 'It is obvious that ' + super().say(stuff)
在任何一种情况下, ae.say("something")
都应该返回:
"It is obvious that eric says: I believe that eric says: something"
这是因为父类是Professor
,而不是Person
。
同样,在你的课堂上,你应该做:
def lecture(self, stuff):
return 'I believe that ' + super(Lecturer, self).say(self, stuff) # or the Python3 version if you're using that
但是,你想要的是什么并不是很清楚。
这应该是:
class ArrogantProfessor( Professor ):
def lecture(self, stuff):
return 'It is obvious that ' + Person.say(self,stuff)
您不必在ArrogantProfessor
定义say()
,因为它已经在Professor
定义,它将使用子类中定义的lecture()
方法。
如果不知道他们想要教你什么,就有点难以说。 一个可能的猜测是,你正在被教授继承,如果他们已超越超级 ,他们可能希望你利用它来让ArrogantProfessor的输出看起来像:
eric says: It is obvious that STUFF
其中STUFF是你传入的字符串。
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)
对于第二部分,正确答案是:
class ArrogantProfessor(Professor):
def lecture(self, stuff):
return 'It is obvious that ' + Lecturer.lecture(self,stuff)
对于第一部分,代码是:
class ArrogantProfessor( Professor ):
def lecture(self, stuff):
return 'It is obvious that ' + Person.say(self,stuff)
对于第二部分,代码是:
class ArrogantProfessor(Professor):
def lecture(self, stuff):
return 'It is obvious that I believe that ' + Person.say(self,stuff)
对于第三部分,代码是:
class Professor(Lecturer):
def say(self, stuff):
return 'Prof. ' + self.name + ' says: ' + self.lecture(stuff)
希望它有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.