Python 子类';方法不';t覆盖其超类方法

Python 子类';方法不';t覆盖其超类方法,python,class,methods,super,Python,Class,Methods,Super,为什么Sloop子类的方法get_info不覆盖其超类的方法 class Boat( object ): def __init__( self, name, loa ): """Create a new Boat( name, loa )""" self.name= name self.loa= loa def get_info(self): print "Boat:", self.name, "Size:", se

为什么Sloop子类的方法get_info不覆盖其超类的方法

class Boat( object ):
    def __init__( self, name, loa ):
        """Create a new Boat( name, loa )"""
        self.name= name
        self.loa= loa
    def get_info(self):
        print "Boat:", self.name, "Size:", self.loa
        print "This is printed correctly"

class Catboat( Boat ):
    def __init__( self, sail_area, * args ):
        """Create a new Catboat( sail_area, name, loa )"""
        super(Catboat,self).__init__( * args )
        self.main_area= sail_area
    def get_info(self):
        #print dir(self)
        print "Boat:", self.name, "Size:", self.loa, "Sail area:", self.main_area
        print "This one also"

class Sloop( Catboat ):
    def __init__( self, jib_area, * args ):
        """Create a new Sloop( jib_area, main_area, name, loa )"""
        super(Sloop,self).__init__( * args )
        self.jib_area= jib_area
    def get_info(self):
        print "Boat:", self.name, "Size:", self.loa, "Sail area:", self.main_area,             "Jib_area:", self.jib_area
        print "This should be printed, but it's not"

boat1 = Boat("Titanic", 20)
catboat1 = Catboat(50, "Titanic", 40)
sloop1 = Sloop(70, 60, "Titanic", 50)

boat1.get_info()
catboat1.get_info()
sloop1.get_info()
'''

印刷品:

船号:泰坦尼克号尺寸:20 这是正确打印的 船:泰坦尼克号尺寸:40帆面积:50 这个也 船:泰坦尼克号尺寸:50帆面积:60 这个也

预期:

船号:泰坦尼克号尺寸:20 这是正确打印的 船:泰坦尼克号尺寸:40帆面积:50 这个也 船:泰坦尼克号尺寸:50帆面积:60三角帆面积:70 这应该打印出来,但不是


如果问题中给出的缩进是正确的,则子类没有get_info()函数

显然你的缩进应该是错误的。你确定它是正确的,而不是如本问题所示吗?是的,确实缩进是错误的。但是,在我的IDE中它是正确的,打印输出如上所示。问题在于缩进。代码是从internet复制的,4-空格与制表符缩进混淆了-因此导致了这个问题。上面的代码是正确的,并且按预期打印出来。您使用的是哪个IDE?使用带有标准值(通常为2,4)的“展开选项卡”选项。这将帮助您保持所有代码的一致性。Geany。这是一个好主意。谢谢。