Python中的类继承

12

我正在解决这个问题:

考虑以下类层次结构:

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)

如此编写的代码在使用傲慢教授类时会导致无限循环。

更改傲慢教授的定义,以实现以下行为:

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’) 的解决方案中是不是打错了,或者真的缺少代词“我”? - L3viathan
@L3viathan 那是个打字错误。 - sid597
9个回答

7

您应该使用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)

4

给定以下内容:

class ArrogantProfessor(Professor): 

但是你却做了这件事情:
class ArrogantProfessor(Person): 

由此导致的成绩减半。

实际上,我最初使用了“Professor”作为参数,但那并没有起作用,所以我将其更改为“Person”。 - sid597
1
这就是@johnsmith提出的任务目标啊!让你思考如何将其与“教授”一起使用。好问题,顺便点个赞。 - gsamaras

2
他可能希望你实际获得父类。这样做的方法很简单。
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

不过,您想要什么并不是很清楚。

这段内容与IT技术无关。

2

这应该是:

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

您不需要在ArrogantProfessor中定义say(),因为它已经在Professor中定义,并且它将使用子类中定义的lecture()方法。


2
作为以前的编码作业评分员,我认为您应该在不将ArrogantProfessor变成一个简单的Person的情况下输出所需的结果。毕竟,类名表明它仍应是Professor的子类。

2

不知道他们想要教你什么,所以很难说。可能是他们想教你继承,如果他们已经讲解了super,那么他们可能希望你利用它使ArrogantProfessor的输出看起来像:

eric says: It is obvious that STUFF

STUFF是您传递的字符串。


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)

你的回答应该有上下文,而不仅仅是代码。 - Jeff

0

关于第二部分,正确答案是:

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

0

第一部分的代码是:

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)

希望它有用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接