【发布时间】:2016-06-28 02:31:49
【问题描述】:
我正在解决这个问题:
考虑以下类的层次结构:
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)正如所写,这段代码在使用 嚣张教授班。
更改 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 新手,前段时间学习了类。)
【问题讨论】:
-
这是
le.lecture(‘the sky is blue’)解决方案中的拼写错误,还是真的缺少代词“I”? -
@L3viathan 打错了
标签: python python-2.7 class oop class-hierarchy