Class 继承于 python
Class inheritance in python
我正在解决这个问题:
Consider the following hierarchy of classes:
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)
As written, this code leads to an infinite loop when using the
Arrogant Professor class.
Change the definition of ArrogantProfessor so that the following
behavior is achieved:
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()
而不是硬连接 class 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)
作为编码硬件的前评分员,我认为,您应该已经产生了所需的输出,而不是使 ArrogantProfessor
仅仅是 Person
。毕竟class这个名字表示还是应该subclassProfessor
.
他可能想让你真正得到父 class。方法很简单。
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")
应该 return:
"It is obvious that eric says: I believe that eric says: something"
这是因为父 class 是 Professor
,而不是 Person
。
同样,在你的演讲中class,你应该这样做:
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):
但你这样做了:
class ArrogantProfessor(Person):
这导致成绩减半。
这应该是:
class ArrogantProfessor( Professor ):
def lecture(self, stuff):
return 'It is obvious that ' + Person.say(self,stuff)
你不必在ArrogantProfessor
中定义say()
,因为它已经在Professor
中定义了,它将使用lecture()
中定义的方法child class.
在不知道他们想教你什么的情况下很难说。一个可能的猜测是,你正在学习继承,如果他们已经讲过 super,他们很可能希望你利用它来让 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)
希望有用
我正在解决这个问题:
Consider the following hierarchy of classes:
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)
As written, this code leads to an infinite loop when using the Arrogant Professor class.
Change the definition of ArrogantProfessor so that the following behavior is achieved:
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()
而不是硬连接 class 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)
作为编码硬件的前评分员,我认为,您应该已经产生了所需的输出,而不是使 ArrogantProfessor
仅仅是 Person
。毕竟class这个名字表示还是应该subclassProfessor
.
他可能想让你真正得到父 class。方法很简单。
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")
应该 return:
"It is obvious that eric says: I believe that eric says: something"
这是因为父 class 是 Professor
,而不是 Person
。
同样,在你的演讲中class,你应该这样做:
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):
但你这样做了:
class ArrogantProfessor(Person):
这导致成绩减半。
这应该是:
class ArrogantProfessor( Professor ):
def lecture(self, stuff):
return 'It is obvious that ' + Person.say(self,stuff)
你不必在ArrogantProfessor
中定义say()
,因为它已经在Professor
中定义了,它将使用lecture()
中定义的方法child class.
在不知道他们想教你什么的情况下很难说。一个可能的猜测是,你正在学习继承,如果他们已经讲过 super,他们很可能希望你利用它来让 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)
希望有用