【问题标题】:Class inheritance in pythonpython中的类继承
【发布时间】: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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    它被赋予:

    class ArrogantProfessor(Professor): 
    

    但你这样做了:

    class ArrogantProfessor(Person): 
    

    这导致成绩减半。

    【讨论】:

    • 其实我一开始是用Professor作为论据,但是没用,所以我把它改成了Person
    • 这是@johnsmith 分配的目标!让您思考如何使它与Professor 一起工作。好问题顺便说一句,你得到了我的支持。
    【解决方案3】:

    我认为,作为以前的编码硬件分级员,您应该已经产生了所需的输出,而无需将ArrogantProfessor 变成仅仅是Person。毕竟,类名表明它仍然应该是Professor的子类。

    【讨论】:

      【解决方案4】:

      他可能希望你真正获得父类。方法很简单。

      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
      

      不过,还不清楚你想要什么。

      【讨论】:

        【解决方案5】:

        在不知道他们想教你什么的情况下很难说。一个可能的猜测是你被教导继承,如果他们已经超过super,他们很可能希望你利用它来让 ArrogantProfessor 的输出看起来像:

        eric says: It is obvious that STUFF
        

        其中 STUFF 是您传入的字符串。

        【讨论】:

          【解决方案6】:

          这应该是:

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

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

          【讨论】:

            【解决方案7】:
                 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)
            

            【讨论】:

            • 你的答案应该有上下文,而不仅仅是代码。
            【解决方案8】:

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

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

            【讨论】:

              【解决方案9】:

              第一部分的代码是:

              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)
              

              希望有用

              【讨论】:

                猜你喜欢
                • 2015-09-04
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-11-03
                相关资源
                最近更新 更多